🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.functional
Article #193

Re: About function middle using primitive recursion

#193
From: Fergus James HENDERSON
Date: Wed, 02 Sep 1992 19:55
36 lines
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!
Technical Details
Message-ID: <9224705.24159@mulga.cs.mu.OZ.AU>
Path: arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
References: <6006@mtecv2.mty.itesm.mx>

Show Full Headers
Complete Article Headers
Newsgroups: comp.lang.functional
Path: arc1-ca.pugleaf.net!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