[Haskell-beginners] Equivalence (or not) of lists

Lawrence Wickert skippy_lou at hotmail.com
Sat Nov 12 20:20:19 UTC 2016


Hello all,

I am a rank beginner to functional languages.  Working through Lipovaca's book, up to Chapter 3.

Ok, setup this function in editor and compiled:

length' :: (Num b) => [a] -> b
length' [] = 0
length' (_:xs) = 1 + length' xs


skippy at skippy:~$ ghci
GHCi, version 7.10.3: http://www.haskell.org/ghc/  :? for help
Prelude> :l baby
[1 of 1] Compiling Main             ( baby.hs, interpreted )
Ok, modules loaded: Main.
*Main> length' [1,2,3]
3
*Main> 1:2:3:[]
[1,2,3]
*Main> length' 1:2:3:[]

<interactive>:5:9:
    Could not deduce (Num [a0]) arising from the literal '1'
    from the context (Num a)
      bound by the inferred type of it :: Num a => [a]
      at <interactive>:5:1-16
    The type variable 'a0' is ambiguous
    In the first argument of 'length'', namely '1'
    In the first argument of '(:)', namely 'length' 1'
    In the expression: length' 1 : 2 : 3 : []
*Main>


Obviously, there is something I don't understand about the apparent non-equivalence of the lists [1,2,3] and 1:2:3:[]I am guessing that the solution is contained in that error message but I can't quite decipher it.

Thanks for any help.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20161112/f503d1ce/attachment.html>


More information about the Beginners mailing list