🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.functional
Article #195

Re: About function middle using primitive recursion

#195
From: Mark Ramaer
Date: Thu, 03 Sep 1992 10:22
47 lines
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
Technical Details
Message-ID: <1992Sep3.122241@cs.utwente.nl>
Path: arc1-ca.pugleaf.net!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
References: <6006@mtecv2.mty.itesm.mx> <9224705.24159@mulga.cs.mu.OZ.AU>

Show Full Headers
Complete Article Headers
Newsgroups: comp.lang.functional
Path: arc1-ca.pugleaf.net!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
From: ramaer@cs.utwente.nl (Mark Ramaer)
Subject: Re: About function middle using primitive recursion
Message-ID: <1992Sep3.122241@cs.utwente.nl>
Keywords: help
Sender: usenet@cs.utwente.nl
Nntp-Posting-Host: utis143
Organization: University of Twente, Dept. of Computer Science
References: <6006@mtecv2.mty.itesm.mx> <9224705.24159@mulga.cs.mu.OZ.AU>
Date: Thu, 3 Sep 1992 10:22:41 GMT
Lines: 47