# Equivalence (or not) of lists

5 messages
Open this post in threaded view
|

## Equivalence (or not) of lists

 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@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:[] :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 :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 [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Open this post in threaded view
|

## Re: Equivalence (or not) of lists

 On Sat, Nov 12, 2016 at 08:20:19PM +0000, Lawrence Wickert wrote: > Hello all, > > I am a rank beginner to functional languages.  Working through > Lipovaca's book, up to Chapter 3. > > [...] > > *Main> length' 1:2:3:[] Hello Lawrence,     remember that function application has precedence over operators! So writing:     *Main> length' 1:2:3:[] is equivalent to writing     *Main> (length' 1) :2:3:[] (which is not what you want). If you add parentheses, your expression works again!     *Main> length' (1:2:3:[]) _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Open this post in threaded view
|

## Re: Equivalence (or not) of lists

 In reply to this post by Lawrence Wickert 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@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:[] :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 :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 [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ``` _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners