25.10.2015 Views

Write You a Haskell Stephen Diehl

1kEcQTb

1kEcQTb

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

For instance the following toplevel pattern for the xor function is transformed into the following nested<br />

set of case statements:<br />

-- Frontend<br />

xor False False = False;<br />

xor False True = True;<br />

xor True False = True;<br />

xor True True = False;<br />

-- Desugared<br />

xor :: Bool -> Bool -> Bool<br />

xor = \_a _b -> case _a of {<br />

False -> case _b of {<br />

False -> False;<br />

True -> True<br />

};<br />

True -> case _b of {<br />

False -> True;<br />

True -> False<br />

}<br />

}<br />

Constructor Patterns<br />

Toplevel declarations in the frontend language can consist of patterns for on the right-hand-side of the<br />

declaration, while in the Core language these are transformed into case statements in the body of the<br />

function.<br />

-- Frontend<br />

f (Left l) = a<br />

f (Right r) = b<br />

-- Desugared<br />

f x = case x of<br />

Left l -> a<br />

Right r -> b<br />

Nested Patterns<br />

e frontend language also allows nested constructors in a single pattern, while in the Core language<br />

these are expanded out into two case statements which scrutinize only one level of pattern.<br />

-- Frontend<br />

f x = case x of<br />

Just (Just y) -> y<br />

112

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!