Equivalence (or not) of lists

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Equivalence (or not) of lists

Lawrence Wickert

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:[]

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

Re: Equivalence (or not) of lists

Francesco Ariis
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
Reply | Threaded
Open this post in threaded view
|

Re: Equivalence (or not) of lists

Sylvain Henry-2
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:[]

<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
[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
Reply | Threaded
Open this post in threaded view
|

Equivalence (or not) of lists

Lawrence Wickert
In reply to this post by Lawrence Wickert

Thanks, Sylvain.  That works and I think I understand the difference now.

Larry



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Equivalence (or not) of lists

Chris Sasarak
In reply to this post by Francesco Ariis

In this case it's not too bad adding parens to make:

   length' (1:2:3:[])

But in the future sometimes it can get harder, so we have a handy
function '$' which is just function application, but it has the lowest
precedence. What this means is everything on its right side is evaluated
and then applied to the function on its left. Your code would look like
this:

    length' $ 1:2:3:[]
   
In some cases this can be more readable.

Best,
Chris

Francesco Ariis writes:

> 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

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners