From: daisy
Subject: increment functions
Date: 
Message-ID: <2206ecf5.0305300840.785ab2cc@posting.google.com>
Hi everyone. I am new to LISP and don't really have a clue what I'm
going, so I hope on of you can help me:
Here is my assignment:
write a LISP function named INCR-POSITIVE which will accept a list and
produce a new list. The new list will keep the numeric, positive atoms
at the top level and remove everything else. In addition, it will
increment these values.

Here is what I have so far:

(defun INCR-POSITIVE (x)
  (cond
     ((null x) nil) ;return a nil at end of lists
     ((listp (car x)) (cons (INCR-POSITIVE (car x)) (INCR-POSITIVE
(cdr x))))
     (plusp (car x)) (cons 


I don't know what to do next or if it is even right so far....
HELP!!

daisy<········@hotmail.com>

From: Nitin Madnani
Subject: Re: increment functions
Date: 
Message-ID: <bb83j9$on4$1@grapevine.wam.umd.edu>
On Fri, 30 May 2003 12:40:19 -0400, daisy wrote:

> Hi everyone. I am new to LISP and don't really have a clue what I'm
> going, so I hope on of you can help me: Here is my assignment: write a
> LISP function named INCR-POSITIVE which will accept a list and produce a
> new list. The new list will keep the numeric, positive atoms at the top
> level and remove everything else. In addition, it will increment these
> values
[SNIP]

Hi daisy

Is this what you are looking for:

>> (incr-positive '((1 -1) (2 -2) (3 -3) (4 -4)))

(2 3 4 5)

Then for this you can use these two function:

;This functions flattens any list
(defun flatten (x)
  (cond ((null x) nil)
	((atom x) (list x))
	(t (append (flatten (car x)) (flatten (cdr x))))))

;May be what you need?
(defun incr-positive (x)
  (let* ((flatlist (flatten x))
	 (plusflatlist (remove-if-not #'plusp flatlist)))
    (mapcar #'(lambda (x) (+ x 1)) plusflatlist)))

HTH
Nitin
From: Nikodemus Siivola
Subject: Re: increment functions
Date: 
Message-ID: <bb84f2$5hq37$1@midnight.cs.hut.fi>
daisy <········@hotmail.com> wrote:
> write a LISP function named INCR-POSITIVE which will accept a list and
> produce a new list. The new list will keep the numeric, positive atoms
> at the top level and remove everything else. In addition, it will
> increment these values.

Clues: 

 - From the point of view of your function the list it is given can
   contain only two kinds of things: numbers which need to be incremented,
   and other things which need to be pruned.

 - If you can't do all of it at once, do it in parts. Either:

   * First write a function that prunes that non-numbers, but leaves the
     numbers as is. Then add the incrementation.

   * First wirte a function that increments the numbers, but does not
     prune the non-numbers. Then add pruning.

 - A recursive solution is fine, and will work, but consider
   alternatives. Read up on DOLIST, PUSH, and REVERSE. Or FIND-IF and
   MAPCAR.

 - If you use a recursive solution, make certain that
  
   > (incr-positive '((1 2) 3 4 5)) => (4 5 6)

   , not ((2 3) 4 5 6). You current code looks like it will soon suffer
   from this...


Cheers,

  -- Nikodemus
From: Kenny Tilton
Subject: Re: increment functions
Date: 
Message-ID: <3ED79A4E.8080903@nyc.rr.com>
daisy wrote:
> Hi everyone. I am new to LISP and don't really have a clue what I'm
> going, so I hope on of you can help me:
> Here is my assignment:
> write a LISP function named INCR-POSITIVE which will accept a list and
> produce a new list. The new list will keep the numeric, positive atoms
> at the top level and remove everything else.

The last sentence (perhaps mistakenly) specifies that "numeric, positive 
atoms" that are not at the top level should be removed. But from your 
code it seems you are trying to preserve those. ie, when you wrote "keep 
at the top level" you meant "collect (at any depth) and return as one 
flat list (so everybody ends up at the top level)".

Just checking, cuz as I said your code does suggest that is what you are 
attempting.

> In addition, it will
> increment these values.
> 
> Here is what I have so far:
> 
> (defun INCR-POSITIVE (x)
>   (cond
>      ((null x) nil) ;return a nil at end of lists
>      ((listp (car x)) (cons (INCR-POSITIVE (car x)) (INCR-POSITIVE
> (cdr x))))

What if a recursive call to incr-positive returns a list? Evaluate the 
following to see why you do not want:

   (cons (list 2 3)(list 4 5))

...but a function that makes one list out of two. (Which function is 
left as an exercise.)

>      (plusp (car x)) (cons 

What if (car x) is not a number? Handle that case before this one.

As for what to write next, hey, you did pretty good handling the case of 
(listp (car x)), except for the use of CONS instead of ????. You want to 
use a parallel solution here: do what you need to do with the positive 
number and return that in a list with the rest of the input recursively 
processed.

> 
> 
> I don't know what to do next or if it is even right so far....

It's actually a good start, you just panicked. :) It happens.

-- 

  kenny tilton
  clinisys, inc
  http://www.tilton-technology.com/
  ---------------------------------------------------------------
"Everything is a cell." -- Alan Kay
From: Lieven Marchand
Subject: Re: increment functions
Date: 
Message-ID: <87n0h4s6cl.fsf@wyrd.be>
········@hotmail.com (daisy) writes:

> Hi everyone. I am new to LISP and don't really have a clue what I'm
> going, so I hope on of you can help me:
> Here is my assignment:
> write a LISP function named INCR-POSITIVE which will accept a list and
> produce a new list. The new list will keep the numeric, positive atoms
> at the top level and remove everything else. In addition, it will
> increment these values.
> 
> Here is what I have so far:
> 
> (defun INCR-POSITIVE (x)
>   (cond
>      ((null x) nil) ;return a nil at end of lists
>      ((listp (car x)) (cons (INCR-POSITIVE (car x)) (INCR-POSITIVE
> (cdr x))))

Here you recurse when an item in the list is itself a list. Is it then
in top level position in the terminology of your assignment?

>      (plusp (car x)) (cons 

PLUSP isn't defined for arguments other than real numbers, so here you
could call (plusp 'foo) which would give an error.

A loopy solution is

(defun incr-positive (list)
  (loop for item in list
        when (and (realp item) (plusp item))
        collect (1+ item)))

-- 
Jane - Daria? Come on, the neighbors are starting to talk.
Daria - Um... good. Soon they'll progress to cave drawings and civilization 
will be on its way.
From: Henrik Motakef
Subject: Re: increment functions
Date: 
Message-ID: <87k7c82uv4.fsf@interim.henrik-motakef.de>
········@hotmail.com (daisy) writes:

> Hi everyone. I am new to LISP and don't really have a clue what I'm
> going, so I hope on of you can help me:
> Here is my assignment:
> write a LISP function named INCR-POSITIVE which will accept a list and
> produce a new list. The new list will keep the numeric, positive atoms
> at the top level and remove everything else. In addition, it will
> increment these values.

(I assume you are supposed to use the crippled teaching style of
"LISP" instead of full Common Lisp, i.e. always use recursion, COND is
the only control structure, etc. Otherwise you could just use

(mapcar #'1+ (remove-if-not #'(lambda (element) 
                                (and (realp element) (plusp element)))
                             list))

or even

(loop for element in list
      when (and (realp element) (plusp element))
      collect (1+ element))

But learning about TIMTOWTDI is probably not the course objective :-)

> Here is what I have so far:
>
> (defun INCR-POSITIVE (x)
>   (cond
>      ((null x) nil) ;return a nil at end of lists
>      ((listp (car x)) (cons (INCR-POSITIVE (car x))
>                             (INCR-POSITIVE (cdr x))))
>      (plusp (car x)) (cons 
>

I don't think you are completely on the right track. Checking for the
end of the list is correct of course, but the second clause looks
strange.

If I read the assignment correctly, you are not supposed to care about
sublists, i.e. '(1 2 (3 4) 5) should become (2 3 6), because (3 4) is
not numeric, and its elements are not at the top level. So why check
for (listp (car ...)) at all?

What you need, after the '(null x)' clause, are two other
clauses:
One to use if (car x) is a positive number (and you should probably
check for numericalness explicitly, because (plusp 'not-a-number) is
an error) that returns a list consed up from the incremented number
and the result of applying INCR-POSITIVE recursivly on (cdr x), and
one for all other cases that throws away (car x) and recurses
directly.

hth
Henrik
From: Wolfhard Buß
Subject: Re: increment functions
Date: 
Message-ID: <m3wug7g4ll.fsf@buss-14250.user.cis.dfn.de>
daisy writes:

> Hi everyone. I am new to LISP and don't really have a clue what I'm
> going, so I hope on of you can help me:
> Here is my assignment:
> write a LISP function named INCR-POSITIVE which will accept a list and
> produce a new list. The new list will keep the numeric, positive atoms
> at the top level and remove everything else. In addition, it will
> increment these values.

 All you need is foo, mapcar-if and zip, of course.

(defun zip (lists)
  (loop with lists = (copy-list lists)
        for row = (loop for cols on lists
                        always (first cols)
                        collect (pop (first cols)) into row
                        finally (return row))
        while row
        collect row))

(defun mapcar-if (function list &rest more-lists)
  (let ((result '()))
    (dolist (args (zip (cons list more-lists)) (nreverse result))
      (multiple-value-bind (value collect-nil-p) (apply function args)
        (if (or value collect-nil-p) (push value result))))))

(defun foo (arg)
  (and (realp arg) (plusp arg) (1+ arg)))

-- 
"Hurry if you still want to see something. Everything is vanishing."
                                       --  Paul C�zanne (1839-1906)