[Haskell-beginners] Equivalence (or not) of lists
Sylvain Henry
sylvain at haskus.fr
Sat Nov 12 20:30:07 UTC 2016
Hi,
> length' 1:2:3:[]
is equivalent to:
> (length' 1):2:3:[]
hence the error.
Try:
> length' (1:2:3:[])
Sylvain
On 12/11/2016 21:20, Lawrence Wickert wrote:
> 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.
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20161112/452b270b/attachment.html>
More information about the Beginners
mailing list