Need some help please.

b.i.mills@massey.ac.nz b.i.mills@massey.ac.nz
Thu, 28 Aug 2003 08:56:58 +1200


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.