There's a small problem with this solution, namely that it requires you to have an Eq instance for the elements in the list. You can fix this by using 'null' from the Prelude. -- Hal Daume III | hdaume@isi.edu "Arrest this man, he talks in maths." | www.isi.edu/~hdaume
-----Original Message----- From: haskell-admin@haskell.org [mailto:haskell-admin@haskell.org] On Behalf Of b.i.mills@massey.ac.nz Sent: Wednesday, August 27, 2003 1:57 PM To: haskell@haskell.org Subject: Re: Need some help please.
What is Nick Name doing? Trolling?
I don't approve of people getting homework done on this emailing list, you learn more from doing, but baiting newbies is worse. You want to give them the idea that Haskell is complex and abtruse?
I need to define a function called safetail; it's like tail except that this one maps the empty list to the empty list. It has to be defined using the following:
Ok, so logicaly, we are saying that we want (safeTail x) to be (tail x) if x is not [], but for x==[] (normally an error) we want (safeTail []) == []. Just write this down in Haskell ...
safeCond x = if x==[] then [] else tail x
Guards are just another way of producing conditional expressions
safeGuard x | x==[] = [] | True = tail x
And pattern matching likewise, for this simple example
safePat [] = [] safePat x = tail x
If you want to test the result ...
tester 0 safe = [(safe [])] tester n safe = (safe [1 .. n]) : (tester (n-1) safe)
call it as (tester 10 safePat) for example, to get a list of some of the outputs from the safePat routine.
Regards,
Bruce.
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell