[Haskell-cafe] Laziness question
Brandon S Allbery KF8NH
allbery at ece.cmu.edu
Sat Jul 31 17:30:54 EDT 2010
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 7/31/10 16:58 , wren ng thornton wrote:
> Brandon S Allbery KF8NH wrote:
>> michael rice wrote:
>>> Are you saying:
>>>
>>> [ head x ] -> [ *thunk* ] and length [ *thunk* ] -> 1, independent of
>>> what *thunk* is, even head [], i.e., *thunk* never needs be evaluated?
>>
>> Exactly. (I was being cagey because the first response was cagey, possibly
>> suspecting a homework question although it seems like an odd time for it.)
>>
>> length not only does not look inside of the thunk, it *can't* look inside
>> it; all it knows is that it has a list, it specifically does *not* know what
>> that list can hold. So the only thing it can do is count the number of
>> "unknown somethings" in the list.
>
> Not entirely true:
>
> stupidlyStrictLength :: [a] -> Integer
> stupidlyStrictLength [] = 0
> stupidlyStrictLength (x:xs) = x `seq` 1 + stupidlyStrictLength xs
Given all the messes seq makes ("hey, go behind the compiler's back and
touch this arbitrary value of arbitrary type"), I generally consider it to
be unsafeSeq :)
- --
brandon s. allbery [linux,solaris,freebsd,perl] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university KF8NH
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAkxUlg4ACgkQIn7hlCsL25U41ACgy88GrDKrhfhNn8IiwYPA92qw
Kn0AnilNyNJsPZXKIp86NEuWW4ECLVuv
=hsLW
-----END PGP SIGNATURE-----
More information about the Haskell-Cafe
mailing list