[Haskell-cafe] Re: Shouldn't this loop indefinitely => take (last [0..]) [0..]

Joachim Breitner mail at joachim-breitner.de
Mon Apr 7 10:44:38 EDT 2008


Hi,

Am Freitag, den 04.04.2008, 22:44 +0100 schrieb Neil Mitchell:
> Hi
> 
> >  We can however write function like this:
> >
> >  eqLengths [] [] = True
> >  eqLengths (x:xs) (y:ys) = eqLengths ys xs
> >  eqLengths _ _ = False
> >
> >  which looks just fine for me.
> 
> I have this defined function. I also have lenEq1, lenGt1, and a few
> other variants. It works, but it just doesn't feel elegant.
> 
> Note: In case anyone gets the wrong impression, I am not suggesting
> lazy naturals be the standard numeric type in Haskell, just that by
> not going that way we have paid a cost in terms of elegance.

How about something like this:

> data Length a = Length [a]
>
> instance Ord (Length a) where
>    compare (Length [])     (Length []) = EQ
>    compare (Length [])     (Length (_:_)) = LT
>    compare (Length (_:_))  (Length []) = GT
>    compare (Length (_:xs)) (Length (_:ys)) = compare (Length xs) (Length ys)
> 
> instance Eq (Length a) where
>    l1 == l2 = compare l1 l2 == EQ

then you can do at least lazy lengths comparisons relatively nice by
writing
> if Length list1 >= Length list2 then print "list1" else print "list2"

(just a quick idea)

Greetings
Joachim

-- 
Joachim Breitner
  e-Mail: mail at joachim-breitner.de
  Homepage: http://www.joachim-breitner.de
  ICQ#: 74513189
  Jabber-ID: nomeata at joachim-breitner.de
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20080407/52a687a9/attachment.bin


More information about the Haskell-Cafe mailing list