Closures/Value capture: Difference between revisions

no edit summary
No edit summary
Line 345:
Output:
<pre>49</pre>
 
=={{header|Haskell}}==
 
Using <code>map</code>:
 
<lang haskell>fs = map (\i _ -> i * i) [1 .. 10]</lang>
 
Using list comprehensions:
 
<lang haskell>fs = [const $ i * i | i <- [1 .. 10]]</lang>
 
Using infinite lists:
 
<lang haskell>fs = take 10 coFs where coFs = [const $ i * i | i <- [1 ..]]</lang>
 
Testing:
 
<lang haskell>> :t fs
fs :: [b -> Integer]
> map ($ ()) fs
[1,4,9,16,25,36,49,64,81,100]
> fs !! 9 $ ()
100
> fs !! 8 $ undefined
81</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
Anonymous user