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

  1. Newsgroups: comp.lang.functional
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!darwin.sura.net!Sirius.dfn.de!math.fu-berlin.de!news.netmbx.de!Germany.EU.net!mcsun!sun4nl!utrcu1!infnews!infnews!ramaer
  3. From: ramaer@cs.utwente.nl (Mark Ramaer)
  4. Subject: Re: About function middle using primitive recursion
  5. Message-ID: <1992Sep3.122241@cs.utwente.nl>
  6. Keywords: help
  7. Sender: usenet@cs.utwente.nl
  8. Nntp-Posting-Host: utis143
  9. Organization: University of Twente, Dept. of Computer Science
  10. References: <6006@mtecv2.mty.itesm.mx> <9224705.24159@mulga.cs.mu.OZ.AU>
  11. Date: Thu, 3 Sep 1992 10:22:41 GMT
  12. Lines: 47
  13.  
  14. In article <6006@mtecv2.mty.itesm.mx>, ojuarez@mtecv2.mty.itesm.mx (Octavio Juarez Espinosa) writes:
  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. In article <9224705.24159@mulga.cs.mu.OZ.AU>, fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
  21. ...
  22. |> the function worked or not. Much better to return either a tagged
  23. |> value (Ok val | Error message) or to raise an exception for even
  24. |> length lists. The following code completely ignores the issue for
  25. |> simplicity.
  26. |> 
  27. |> NB. all these functions are "primitive recursive" in the original poster's
  28. |> sense.
  29. |> 
  30. |> tail' [] = []
  31. |> tail' (x:xs) = xs
  32. |> 
  33. |> middle_odd [] ys = tail' ys
  34. |> middle_odd (x:xs) ys = middle_odd (tail' xs) (tail' ys)
  35. |> 
  36. |> middle l = middle_odd ([] : l) l
  37. |> 
  38.  
  39. Sorry, but I tried out this function and it is not correct.
  40. My attempt is (I return a singleton list on success):
  41.  
  42. middle l = even l l
  43.  
  44. even halfSpeed []     = []
  45. even halfSpeed (x:xs) = odd halfSpeed xs
  46.  
  47. odd (midValue:halfSpeed) []     = [midValue]
  48. odd (void    :halfSpeed) (x:xs) = even halfSpeed xs
  49.  
  50. Test:
  51. Miranda [middle [1..n]|n<-[0..20]]
  52. [[],[1],[],[2],[],[3],[],[4],[],[5],[],[6],[],[7],[],[8],[],[9],[],[10],[]]
  53. Miranda ^D
  54.  
  55. By the way, I did not understand the constraint completely,
  56. what means CRD?  Do you mean CDR, or as we LISP-haters say: tail?
  57. You have to know the length of the list before you can decide what
  58. element to return.
  59.  
  60. Mark Ramaer
  61.