I execute the next code:
(take 10) $! [1,2..]
What is it ? I thought, ghc will yield a termination, beacause i say "evaluate [1,2..] force". But I got the result "[1,2,3,4,5,6,7,8,9,10]".
I execute the next code:
(take 10) $! [1,2..]
What is it ? I thought, ghc will yield a termination, beacause i say "evaluate [1,2..] force". But I got the result "[1,2,3,4,5,6,7,8,9,10]".
Did you type something wrong? It works for me with or without parentheses, under GHCi 6.8.2...
GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help
($Loading package base ... linking ... done.
Prelude> take 10 $! [1..]
[1,2,3,4,5,6,7,8,9,10]
Prelude> (take 10) $! [1..]
[1,2,3,4,5,6,7,8,9,10]
Prelude> (take 10) $! [1,2..]
[1,2,3,4,5,6,7,8,9,10]
The forced evaluation will only ensure its argument [1,2..]
isn't bottom. [1,2..]
matches (1:_)
, so it isn't bottom, and the computation will go on as expected, returning [1,2,3,4,5,6,7,8,9,10]
.
Not sure where you got that single 1
result from; would you mind copy-pasting an excerpt of a GHCi session?
So you expect the list to be fully evaluated. ($!) is implemented in terms of seq
, and seq
"only" evaluates to head normal form according to the docs. It'll only make a difference if the value is undefined
:
Prelude> take 0 undefined
[]
Prelude> take 0 $! undefined
*** Exception: Prelude.undefined
A function is strict in its argument if
f undefined = undefined
This does not imply that the argument is fully evaluated in an eager fashion. What you want is something like DeepSeq.
GHCi, version 6.10.1: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling DeepSeq ( deepSeq.lhs, interpreted )
Ok, modules loaded: DeepSeq.
*DeepSeq> take 1 $!! [1,2,undefined]
*** Exception: Prelude.undefined
*DeepSeq>
Your example with $!!
from DeepSeq
runs forever.