home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / function / 1058 < prev    next >
Encoding:
Text File  |  1992-09-02  |  1.7 KB  |  49 lines

  1. Newsgroups: comp.lang.functional
  2. Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
  3. From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
  4. Subject: Re: About function middle using primitive recursion
  5. Message-ID: <9224705.24159@mulga.cs.mu.OZ.AU>
  6. Keywords: help
  7. Sender: news@cs.mu.OZ.AU
  8. Organization: Computer Science, University of Melbourne, Australia
  9. References: <6006@mtecv2.mty.itesm.mx>
  10. Date: Wed, 2 Sep 1992 19:55:15 GMT
  11. Lines: 36
  12.  
  13. ojuarez@mtecv2.mty.itesm.mx (Octavio Juarez Espinosa) writes:
  14.  
  15. >I am very interested in to receive some version for funtion middle.
  16. >
  17. >(middle list)  return nil if is even number of elements.
  18. >        return the element if is odd the number of elements
  19. >
  20. >Constrains:
  21. >Only one recursion call and only with the CRD of argument.
  22.  
  23. I don't quite understand why you want to return "nil" when the
  24. list has an even number of elements. If you are applying the
  25. function to a list of numbers, then "nil" is not a number, so
  26. it won't work for strongly typed languages; if you are applying
  27. the function to a list of lists, then you can't tell whether
  28. the function worked or not. Much better to return either a tagged
  29. value (Ok val | Error message) or to raise an exception for even
  30. length lists. The following code completely ignores the issue for
  31. simplicity.
  32.  
  33. NB. all these functions are "primitive recursive" in the original poster's
  34. sense.
  35.  
  36. tail' [] = []
  37. tail' (x:xs) = xs
  38.  
  39. middle_odd [] ys = tail' ys
  40. middle_odd (x:xs) ys = middle_odd (tail' xs) (tail' ys)
  41.  
  42. middle l = middle_odd ([] : l) l
  43.  
  44. -- 
  45. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  46. This .signature virus is a self-referential statement that is true - but 
  47. you will only be able to consistently believe it if you copy it to your own
  48. .signature file!
  49.