25.10.2015 Views

Write You a Haskell Stephen Diehl

1kEcQTb

1kEcQTb

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

• A thunk, i.e., the application of a function to values that have not been evaluated yet<br />

• A thunk that is currently being evaluated, which may induce the evaluation of other thunks in the<br />

process<br />

• An expression in weak head normal form, which is only evaluated to the outermost constructor or<br />

lambda abstraction<br />

e runtime has the task of determining which thunks are to be evaluated by the order in which they<br />

are connected to the main function node. is is the essence of all evaluation in <strong>Haskell</strong> and is called<br />

graph reduction.<br />

Self-referential functions are allowed in <strong>Haskell</strong>. For example, the following functions generate infinite<br />

lists of values. However, they are only evaluated up to the depth that is necessary.<br />

-- Infinite stream of 1’s<br />

ones = 1 : ones<br />

-- Infinite count from n<br />

numsFrom n = n : numsFrom (n+1)<br />

-- Infinite stream of integer squares<br />

squares = map (^2) (numsfrom 0)<br />

e function take consumes an infinite stream and only evaluates the values that are needed for the<br />

computation.<br />

take :: Int -> [a] -> [a]<br />

take n _ | n a<br />

undefined :: a<br />

18

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

Saved successfully!

Ooh no, something went wrong!