
Hi Trying to learn a bit about Haskell as general (script) coder (batch, autoit, python). And run into some code I don't get. (could not find the right online doc that explained it.) [Code] length' :: [a] -> Int length' l = len l 0# where len :: [a] -> Int# -> Int len [] a# = I# a# len (_:xs) a# = len xs (a# +# 1#) [/Code] source: http://www.haskell.org/ghc/docs/latest/html/libraries/base/src/GHC-List.html... 1) What is the purpose of the used pound/"#" character here. (Looks like some type-casting, but that's just a wild guess from me here. (I'm not a C~ or Java coder)) 2) kinda the same for the "I#" in the "len [] ..." line. As the length function takes only one return result. (note: its a capital i and not a lowercase L.) 3) Why is it giving a compile error on the "where" line. Error: "Haskell\baby.hs:<lineNr>:9: parse error on input `where'" (while the adjusted code below compiles fine. Tab(4s) indented.) [Code] length' :: [a] -> Int length' l = len l 0 where len :: [a] -> Int -> Int len [] a = a len (_:xs) a = len xs (a + 1) [/Code] Using: WinGHCi 1.0.6 on Windows Xp. (Haskell Platform 2012.2.0.0 windows install) TIA MvGulik

On Tue, Oct 23, 2012 at 4:31 AM, M.v.Gulik
1) What is the purpose of the used pound/"#" character here. (Looks like some type-casting, but that's just a wild guess from me here.
This is not normal Haskell code you're looking at; it's using internals which are normally hidden, and # is not special in normal Haskell code. If you turn on the special meaning (MagicHash) then it usually means that something is unlifted. In this case, 1# is an unboxed Int value: a raw machine word.
2) kinda the same for the "I#" in the "len [] ..." line. As the length
I# is the internal constructor for a normal Int value; the value (1 :: Int) is internally represented as (I# 1#), or the internal Int constructor wrapping a raw machine word.
3) Why is it giving a compile error on the "where" line. Error:
Because # does not have its special meaning in normal Haskell code. http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#ma... this special behavior, and what you need to do to get it. But you should probably not be worrying about it at this point, and almost certainly not using it. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix/linux, openafs, kerberos, infrastructure http://sinenomine.net
participants (2)
-
Brandon Allbery
-
M.v.Gulik