[Haskell-cafe] Newbie question

Brandon Allbery allbery.b at gmail.com
Sat Jan 15 14:49:25 UTC 2022


Actually, I'm going to correct that; I misread. I suspect your real
problem is you entered those on separate lines in ghci, and each one
redefines the previous one instead of augmenting it because ghci can't
know you're going to continue the definition. Either use multi-line
input mode (:{ and :}) or enter them all on one line with semicolons
in place of newlines.

On Sat, Jan 15, 2022 at 9:43 AM Brandon Allbery <allbery.b at gmail.com> wrote:
>
> You've covered 0 elements, 1 element, 3 or more elements, but not 2 elements.
>
> On Sat, Jan 15, 2022 at 9:33 AM Terry Phelps <tgphelps50 at gmail.com> wrote:
> >
> > I'm trying yet again to "get" Haskell. I'm reading Graham Hutton's book "Programming in Haskell", and am working on one of the exercises. Sounds easy enough:
> >
> > Make a function 'last' that returns the last element of a non-empty list.
> >
> > After struggling a while, I looked at the answer: last = head . reverse.
> > I wish I had thought of that. But I kept trying to get my recursive version done, but I can't seem to make ghci happy. (Version 8.10.5, if you care). I wrote:
> >
> > last [] = []
> > (Yes, I know that's not a non-empty list, but I don't want ghci whining about non-exhaustive patterns). Then I added:
> >
> > last [x] = x
> > And I checked the type:
> > :t last
> > last:: [a] -> [a]
> >
> > Yes, that looks right. There seems to be only one other case: 2 or more elements. So I wrote:
> >
> > last (x:y:xs) = last (y:xs)
> >
> > I ran it:
> >
> > Prelude> last [1,2]
> > *** Exception: <interactive>:2:1-27: Non-exhaustive patterns in function last
> >
> > What the heck is the problem? I've covered every possible list, haven't I?
> >
> > Stranger yet, I check the type again:
> >
> > Prelude> :t last
> > last :: [a] -> t
> >
> > The type has changed. And I don't understand what it means. What's the 't'?
> >
> > I'm sure this is a simple beginner error, but I'm really confused. I don't see how it isn't exhaustive, and I don't see why the 3rd clause (or whatever it's called) caused the type to change.
> >
> > Someone help, please.
> > _______________________________________________
> > Haskell-Cafe mailing list
> > To (un)subscribe, modify options or view archives go to:
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> > Only members subscribed via the mailman list are allowed to post.
>
>
>
> --
> brandon s allbery kf8nh
> allbery.b at gmail.com



-- 
brandon s allbery kf8nh
allbery.b at gmail.com


More information about the Haskell-Cafe mailing list