From: Niclas Lars�n
Subject: assignment & "largest element in array"
Date: 
Message-ID: <3KGN4.11796$uJ1.27496@nntpserver.swip.net>
First, I'm a beginner in LISP in case you don't notice. I have two questions
how do I assign a value to a atom secondly, does this function return the
"highest" atom in any list?

(defun maxx (l)
    (cond ((atom l)                     l)
        ((> (car l) (cdr l))             (maxx (append (car l) (nthcdr 2
l))))
        (t                                     (maxx (cdr l)))
  )
)

(maxx '(a b c d))               /* symbols definition is void: maxx  ??   */


Please help or if you know any itroductionary texts about common LISP.
/Niclas

From: Barry Margolin
Subject: Re: assignment & "largest element in array"
Date: 
Message-ID: <U3HN4.90$my3.2172@burlma1-snr2>
In article <·····················@nntpserver.swip.net>,
Niclas Lars�n <·············@swipnet.se> wrote:
>First, I'm a beginner in LISP in case you don't notice. I have two questions
>how do I assign a value to a atom secondly, does this function return the

You don't assign values to atoms, you assign values to variables, and you
do it with SETQ.

>"highest" atom in any list?

It's close, but has some bugs; see below.

>
>(defun maxx (l)
>    (cond ((atom l)                     l)
>        ((> (car l) (cdr l))             (maxx (append (car l) (nthcdr 2
>l))))

(cdr l) should be (cadr l) or (second l).  And APPEND requires the first
argument to be a list; you should use CONS:

(maxx (cons (car l) (cddr l)))

Also, before this you need to handle the case where there's only one
element in the list (this will happen when the recursion bottoms out).  So
before the above line, you need:

  ((null (cdr l))  (car l))



>        (t                                     (maxx (cdr l)))
>  )
>)
>
>(maxx '(a b c d))               /* symbols definition is void: maxx  ??   */

I don't know why you're getting that error.  The complaint you *should* be
getting is that you can't use > with symbols.  Your function expects a list
of numbers.  If A, B, C, and D are variables whose values are numbers, you
need to write:

(maxx (list a b c d))

-- 
Barry Margolin, ······@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: SRS
Subject: Re: assignment & "largest element in array"
Date: 
Message-ID: <8ebupu$k6d$1@nnrp1.deja.com>
In article <·····················@nntpserver.swip.net>,
  "Niclas Lars�n" <·············@swipnet.se> wrote:
> how do I assign a value to a atom

Use setf or setq (I personally only use setf).

> secondly, does this function return the
> "highest" atom in any list?

If didn't read the function, if you want to extract the highest number
from a list (or array) you should use the following idiom:

(reduce (function max) list)

--SRS



Sent via Deja.com http://www.deja.com/
Before you buy.