[Haskell-beginners] why is this type wrong

Sumit Sahrawat, Maths & Computing, IIT (BHU) sumit.sahrawat.apm13 at iitbhu.ac.in
Tue May 12 06:47:21 UTC 2015


I recommending first trying out the `last' function in ghci.

    Prelude> last []
    *** Exception: Prelude.last: empty list
    Prelude> last [1,2]
    2

Instead of using

    last [] = []

You can use

    last [] = error "Empty list"

Where error is defined in a way such that it can take on any type.
In ghci

    Prelude> :type error
    error :: [Char] -> a

On 12 May 2015 at 12:09, Mike Meyer <mwm at mired.org> wrote:

> On May 12, 2015 1:32 AM, "Roelof Wobben" <r.wobben at home.nl> wrote:
> >
> > Hello,
> >
> > To practice recursion I try to make some functions of Data list myself
> on the recursive way.
> >
> > First I will try last.
> >
> > So I did this :
> >
> > -- | Main entry point to the application.
> > module Main where
> >
> > -- | The main entry point.
> > last' :: [a] -> a
> > last' [] = []
> > last' (x:xs) = last xs
> >
> >
> > but now I see this error message :
> >
> > src/Main.hs at 6:12-6:14
> > Couldn't match expected type
> > a
> > with actual type
> > [t0]
> > a
> > is a rigid type variable bound by the type signature for last' :: [a] ->
> a at
> /home/app/isolation-runner-work/projects/112712/session.207/src/src/Main.hs:5:10
> Relevant bindings include last' :: [a] -> a (bound at
> /home/app/isolation-runner-work/projects/112712/session.207/src/src/Main.hs:6:1)
>> >
> > I my oponion I have said that the input is a array and the output a
> string,
>
> Except for saying list instead of array, I'd agree with that. But when you
> write last' [] = [], the output is [], which does not have the expected
> type of strong. So the actual type is a list.
>
> You need to return a string.
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>


-- 
Regards

Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150512/fcc0da37/attachment.html>


More information about the Beginners mailing list