Thread View: comp.lang.functional
3 messages
3 total messages
Started by Octavio Juarez Espinosa
Wed, 02 Sep 1992 15:38
About function middle using primitive recursion
Author: Octavio Juarez Espinosa
Date: Wed, 02 Sep 1992 15:38
Date: Wed, 02 Sep 1992 15:38
24 lines
876 bytes
876 bytes
Hi, 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. Thanks in advance. Sincerely, Octavio Juarez Espinosa. ojuarez@mtecv2.mty.itesm.mx -- Octavio Juarez Espinosa Gabriel Valerio 1134 Saltillo, Coahuila CP 25000 Mexico email ojuarez@mtecv2.mty.itesm.mx
Re: About function middle using primitive recursion
Author: Fergus James HENDERSON
Date: Wed, 02 Sep 1992 19:55
Date: Wed, 02 Sep 1992 19:55
36 lines
1807 bytes
1807 bytes
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!
Re: About function middle using primitive recursion
Author: Mark Ramaer
Date: Thu, 03 Sep 1992 10:22
Date: Thu, 03 Sep 1992 10:22
47 lines
2199 bytes
2199 bytes
In article <6006@mtecv2.mty.itesm.mx>, 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 In article <9224705.24159@mulga.cs.mu.OZ.AU>, fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes: ... |> 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 |> Sorry, but I tried out this function and it is not correct. My attempt is (I return a singleton list on success): middle l = even l l even halfSpeed [] = [] even halfSpeed (x:xs) = odd halfSpeed xs odd (midValue:halfSpeed) [] = [midValue] odd (void :halfSpeed) (x:xs) = even halfSpeed xs Test: Miranda [middle [1..n]|n<-[0..20]] [[],[1],[],[2],[],[3],[],[4],[],[5],[],[6],[],[7],[],[8],[],[9],[],[10],[]] Miranda ^D By the way, I did not understand the constraint completely, what means CRD? Do you mean CDR, or as we LISP-haters say: tail? You have to know the length of the list before you can decide what element to return. Mark Ramaer
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads