home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.functional
- Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
- From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
- Subject: Re: About function middle using primitive recursion
- Message-ID: <9224705.24159@mulga.cs.mu.OZ.AU>
- Keywords: help
- Sender: news@cs.mu.OZ.AU
- Organization: Computer Science, University of Melbourne, Australia
- References: <6006@mtecv2.mty.itesm.mx>
- Date: Wed, 2 Sep 1992 19:55:15 GMT
- Lines: 36
-
- ojuarez@mtecv2.mty.itesm.mx (Octavio Juarez Espinosa) writes:
-
- >I am very interested in to receive some version for funtion middle.
- >
- >(middle list) return nil if is even number of elements.
- > return the element if is odd the number of elements
- >
- >Constrains:
- >Only one recursion call and only with the CRD of argument.
-
- I don't quite understand why you want to return "nil" when the
- list has an even number of elements. If you are applying the
- function to a list of numbers, then "nil" is not a number, so
- it won't work for strongly typed languages; if you are applying
- the function to a list of lists, then you can't tell whether
- the function worked or not. Much better to return either a tagged
- value (Ok val | Error message) or to raise an exception for even
- length lists. The following code completely ignores the issue for
- simplicity.
-
- NB. all these functions are "primitive recursive" in the original poster's
- sense.
-
- tail' [] = []
- tail' (x:xs) = xs
-
- middle_odd [] ys = tail' ys
- middle_odd (x:xs) ys = middle_odd (tail' xs) (tail' ys)
-
- middle l = middle_odd ([] : l) l
-
- --
- Fergus Henderson fjh@munta.cs.mu.OZ.AU
- This .signature virus is a self-referential statement that is true - but
- you will only be able to consistently believe it if you copy it to your own
- .signature file!
-