From: ngyat
Subject: a question
Date: 
Message-ID: <3446EFEA.F7460B9E@ucs.isu.edu>
Hi,

I looked at one of the  question on POS+ (which takes a list and return
a list of each element plus its position)

Such that:
(pos+ '(8 7 6 6))

(8 8 8 9)

I tried to write a function by using recursion instead of iteration for
it. However, somehow, I just could get it work at all! If any people
here can do that, just let me know, I 'll appreciate!

Thanks,

From: Matthias Hoelzl (tc)
Subject: Re: a question
Date: 
Message-ID: <87wwjca7pe.fsf@gauss.muc.de>
ngyat <·····@ucs.isu.edu> writes:

> Hi,
> 
> I looked at one of the  question on POS+ (which takes a list and return
> a list of each element plus its position)
> 
> Such that:
> (pos+ '(8 7 6 6))
> 
> (8 8 8 9)
> 
> I tried to write a function by using recursion instead of iteration for
> it. However, somehow, I just could get it work at all! If any people
> here can do that, just let me know, I 'll appreciate!

How does one write a recursive solution to such a problem?  Well, the
simplest pattern of a function that applies a mapping to each element
of a list is:

(defun transform-list (list)
  (if (null list)
       <return some appropriate value>
       (cons (<apply some transformation to> (car list))
             (transform-list (cdr list)))))

What is the appropriate value to return when the list is empty?  This
is easy.  Surely (pos+ '()) => ().

And how do we transform (car list)?  If we know at which position in
the list we are it is easy: the transformation for the nth element is
just to add n to its value.  To keep track of the position in the list
we need to introduce a second parameter and arrive at

(defun pos+ (list current-position)
  (if (null list)
      '()
      (cons (+ (car list) current-position)
            (pos+ (cdr list) (1+ current-position)))))

When you call this version of `pos+' with the initial value 0 for
`current-position' you obtain the desired result.  However the
parameter `current-position' was not present in the original version
of `pos+' and so it is better to wrap up this recursion in a helper
function:

(defun pos+ (list)
  (labels ((rec-pos+ (list current-position)
	     (if (null list)
		 '()
		 (cons (+ (car list) current-position)
		       (rec-pos+ (cdr list) (1+ current-position))))))
    (rec-pos+ list 0)))

A very good book to introduce you to recursion is

The Little Schemer, fourth edition
Daniel P. Friedman and Matthias Felleisen
The MIT Press
ISBN 0-262-56099-2

Hope this helps, 

  Matthias H�lzl
From: Jussi Piitulainen
Subject: a cursion Re: a question
Date: 
Message-ID: <oiwwjc1ttx.fsf@tuuri.helsinki.fi>
ngyat <·····@ucs.isu.edu> writes:

> I looked at one of the  question on POS+ (which takes a list and return
> a list of each element plus its position)

> I tried to write a function by using recursion instead of iteration for
> it. However, somehow, I just could get it work at all! If any people
> here can do that, just let me know, I 'll appreciate!

It's always that time of year. Pardon my grumpiness.

(defun positulate (posit numpers positsion)
   (if (consp numpers)
      (cons (funcall posit (car numpers) positsion)
         (positulate posit (cdr numpers) (+ positsion 1)))
      numpers))

(defun pos+ (numpers)
   ;; get to choose the leftmost position -- choose zeroth
   (positulate (function +) numpers 0))

You get to write the documentation strings.

-- 
From: Jesse Paulsen
Subject: Re: a question
Date: 
Message-ID: <6285lk$emo@tower.itis.com>
In article <·················@ucs.isu.edu>, ngyat  <·····@ucs.isu.edu> wrote:
>I looked at one of the  question on POS+ (which takes a list and return
>a list of each element plus its position)
>
>I tried to write a function by using recursion instead of iteration for
>it. However, somehow, I just could get it work at all! If any people
>here can do that, just let me know, I 'll appreciate!

Given that I believe this is from Graham's ACL, my very inexperienced 
solution, using only functions taught up to that point in the book, is:

(defun pos+ (lst)
  (if (null lst)
      nil
    (cons (car lst) (mapcar #'(lambda (x) (+ x 1))
                            (pos+ (cdr lst))))))
From: John Arley Burns
Subject: Re: a question
Date: 
Message-ID: <wzoh4papzb.fsf@urquan-kohr-ah.mesas.com>
ngyat <·····@ucs.isu.edu> writes:

> Hi,
> 
> I looked at one of the  question on POS+ (which takes a list and return
> a list of each element plus its position)
> 
> Such that:
> (pos+ '(8 7 6 6))
> 
> (8 8 8 9)
> 
> I tried to write a function by using recursion instead of iteration for
> it. However, somehow, I just could get it work at all! If any people
> here can do that, just let me know, I 'll appreciate!
> 
> Thanks,

(defun inc-lst (lst)
  (cond
   ((atom lst) nil)
   (t (cons (1+ (car lst)) (inc-lst (cdr lst))))))

(inc-lst '(1 2 3 4))

(defun pos+ (lst)
  (cond
   ((atom lst) nil)
   (t (cons (car lst) (pos+ (cdr (inc-lst lst)))))))

(pos+ '(8 7 6 6))

The key lies in using two functions and traditional list recursion.
That's in two functions, it's cleaner, but you can do it in one with a
lambda exp. Note that this is the first lisp code I've ever written -
it works, but there are probably more elegant ways (like with a map
function). And if this is some homework assignment you're cheating on,
let God be the judge.

pax et bonum