From: Jame
Subject: Lisp help
Date: 
Message-ID: <3e6fe714.69.16838@cs.rmit.edu.au>
Any one show me the source code for mapcar function in Lisp
If i have a list L with some level such as "((1 3) (31
(33))(32 43 23)) and i just one to get the first element of
each level, how can i do it with recursive function instead
of using
(mapcar (first L)).
How to check whether the elements in the list is numeric or
symbol.
Thank for your help

From: Peter Seibel
Subject: Re: Lisp help
Date: 
Message-ID: <m3u1e73nm5.fsf@localhost.localdomain>
Jame writes:

> Any one show me the source code for mapcar function in Lisp
> If i have a list L with some level such as "((1 3) (31
> (33))(32 43 23)) and i just one to get the first element of
> each level, how can i do it with recursive function instead
> of using
> (mapcar (first L)).
> How to check whether the elements in the list is numeric or
> symbol.

Why don't you show us what you've got so far; we can probably help you
better that way and you'll understand it better in the end too.

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

  The intellectual level needed   for  system design is  in  general
  grossly  underestimated. I am  convinced  more than ever that this
  type of work is very difficult and that every effort to do it with
  other than the best people is doomed to either failure or moderate
  success at enormous expense. --Edsger Dijkstra
From: unknown
Subject: Re: Lisp help
Date: 
Message-ID: <3e7080c5.50af.16838@cs.rmit.edu.au>
Here is the function that i need to sum all the number found
in the list
(defun add (L)
  (cond ((null L)0))
        (T (+ (first L) (add (rest L))))))
So this is the version that assume the user enter the
correct syntax, i.e (add '(1 3 4 5)) not (add '(1 3 v a)).
how can i   check the list is fullfill with number only and
discard the list contains letters or symbols. Is it possible
to use mapcar to apply numberp for all element in the list
to solve this problem?

> Jame writes:
>
> > Any one show me the source code for mapcar function in
> > Lisp If i have a list L with some level such as "((1 3)
> > (31 (33))(32 43 23)) and i just one to get the first
> > element of each level, how can i do it with recursive
> > function instead of using
> > (mapcar (first L)).
> > How to check whether the elements in the list is numeric
> > or symbol.
>
> Why don't you show us what you've got so far; we can
> probably help you better that way and you'll understand it
> better in the end too.
> -Peter
>
> --
> Peter Seibel
> ·····@javamonkey.com
>   The intellectual level needed   for  system design is
> in  general
>   grossly  underestimated. I am  convinced  more than ever
> that this
>   type of work is very difficult and that every effort to
> do it with
>   other than the best people is doomed to either failure
> or moderate
>   success at enormous expense. --Edsger Dijkstra
From: Kent M Pitman
Subject: Re: Lisp help
Date: 
Message-ID: <sfw7kb38l5u.fsf@shell01.TheWorld.com>
unknown writes:

> Here is the function that i need to sum all the number found
> in the list
> (defun add (L)
>   (cond ((null L)0))
>         (T (+ (first L) (add (rest L))))))
> So this is the version that assume the user enter the
> correct syntax, i.e (add '(1 3 4 5)) not (add '(1 3 v a)).
> how can i   check the list is fullfill with number only and
> discard the list contains letters or symbols. Is it possible
> to use mapcar to apply numberp for all element in the list
> to solve this problem?

Sure.

You can, as you note, tell by using NUMBERP whether something is a number.

MAPCAR has to return a list of the same length as its input, so it isn't
good for discarding things.  You could, of course, translate V and A to 0,
since adding 0 isn't going to change the output.

(I'm assuming this is homework so I'm not offering a direct answer.)

An appropriate call to mapcar can change
 (1 3 v a) to (1 3 0 0)
and then you can use your ADD routine to get 4, which is the same as you'd
get if you'd removed the V and A and just ADD'd (1 3).

Incidentally, when you get beyond homework and you start to get into
using _real_ Common Lisp, you'll want to check out the REDUCE function,
which can be made to do most of what your ADD function does in a much
more general way.  (reduce #'+ '(1 3 4 5)) => 13

Again using other operators from Common Lisp (and most Lisp dialects),
the mapper that one usually uses for filtering a list is MAPCAN.  You would
write
 (mapcan #'(lambda (x) (if (numberp x) (list x) '())) '(1 3 v a))
and get back (1 3) because MAPCAN will concatenate the results of each of
the calls to the function; so you have the function return a one-length
list for elements to retain, and a zero-length list for elements not to
retain.  [Be careful, though, because the form of concatenation used is
destructive, so until you're confident about side-effects it's best to always
return a newly-consed list rather than some piece of data that was an
argument to MAPCAN.]

You can also, in CL, use LOOP.   It has its own quite complex syntax,
which you  have to learn separately, but you should know it's an available
option:
 (loop for x in '(1 3 v a)
       when (numberp x)
        collect x) => (1 3)
or
 (loop for x in '(1 3 v a)
       when (numberp x)
        sum x) => 4

Hopefully this information will help you get to the next step.
From: Jame
Subject: Re: Lisp help
Date: 
Message-ID: <3e709134.68fd.16838@cs.rmit.edu.au>
Thank you very much for your information, but i just need to
check the simple thing that is check whether the list
contains only number then return sum of them, otherwise
display error msg that this list is not valid. I am using
mapcar function to check the number in the list however
everytime mapcar apply on each element in the list it will
show an errors message
For example: (add '(1 2 v s d))
--> list is not valid
     list is not valid
      list is not valid

> unknown writes:
>
> > Here is the function that i need to sum all the number
> > found in the list
> > (defun add (L)
> >   (cond ((null L)0))
> >         (T (+ (first L) (add (rest L))))))
> > So this is the version that assume the user enter the
> > correct syntax, i.e (add '(1 3 4 5)) not (add '(1 3 v
> > a)). how can i   check the list is fullfill with number
> > only and discard the list contains letters or symbols.
> > Is it possible to use mapcar to apply numberp for all
> > element in the list to solve this problem?
>
> Sure.
>
> You can, as you note, tell by using NUMBERP whether
> something is a number.
> MAPCAR has to return a list of the same length as its
> input, so it isn't good for discarding things.  You could,
> of course, translate V and A to 0, since adding 0 isn't
> going to change the output.
> (I'm assuming this is homework so I'm not offering a
> direct answer.)
> An appropriate call to mapcar can change
>  (1 3 v a) to (1 3 0 0)
> and then you can use your ADD routine to get 4, which is
> the same as you'd get if you'd removed the V and A and
> just ADD'd (1 3).
> Incidentally, when you get beyond homework and you start
> to get into using _real_ Common Lisp, you'll want to check
> out the REDUCE function, which can be made to do most of
> what your ADD function does in a much more general way.
> (reduce #'+ '(1 3 4 5)) => 13
> Again using other operators from Common Lisp (and most
> Lisp dialects), the mapper that one usually uses for
> filtering a list is MAPCAN.  You would write
>  (mapcan #'(lambda (x) (if (numberp x) (list x) '())) '(1
> 3 v a)) and get back (1 3) because MAPCAN will concatenate
> the results of each of the calls to the function; so you
> have the function return a one-length list for elements to
> retain, and a zero-length list for elements not to retain.
>  [Be careful, though, because the form of concatenation
> used is destructive, so until you're confident about
> side-effects it's best to always return a newly-consed
> list rather than some piece of data that was an argument
> to MAPCAN.]
> You can also, in CL, use LOOP.   It has its own quite
> complex syntax, which you  have to learn separately, but
> you should know it's an available option:
>  (loop for x in '(1 3 v a)
>        when (numberp x)
>         collect x) => (1 3)
> or
>  (loop for x in '(1 3 v a)
>        when (numberp x)
>         sum x) => 4
>
> Hopefully this information will help you get to the next
> step.
From: Peter Seibel
Subject: Re: Lisp help
Date: 
Message-ID: <m3fzpr161m.fsf@localhost.localdomain>
Jame writes:

> Thank you very much for your information, but i just need to check
> the simple thing that is check whether the list contains only number
> then return sum of them, otherwise display error msg that this list
> is not valid. I am using mapcar function to check the number in the
> list however everytime mapcar apply on each element in the list it
> will show an errors message

> For example: (add '(1 2 v s d))
> --> list is not valid
>      list is not valid
>       list is not valid

So leaving aside, for a moment, how to express it in Lisp, how are you
planning to attack this problem? I.e. what algorithm do you want to
use. Because there's probably a way to express it in Lisp.

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

  The intellectual level needed   for  system design is  in  general
  grossly  underestimated. I am  convinced  more than ever that this
  type of work is very difficult and that every effort to do it with
  other than the best people is doomed to either failure or moderate
  success at enormous expense. --Edsger Dijkstra
From: Kenny Tilton
Subject: What do CLisp and CormanLisp have in common?
Date: 
Message-ID: <3E70E31F.2020405@nyc.rr.com>
That's not a trick question. :)

I just got the exact same bug (at runtime, but from a faulty assertion 
generated during compilation of a defstruct accessor) out of compiled 
Cells code under CLisp and CormanLisp.

So...is one an offshoot of the other, or are both offshoots of something 
else? Jes curious, but it also might help me work out a work-around.

-- 

  kenny tilton
  clinisys, inc
  http://www.tilton-technology.com/
  ---------------------------------------------------------------
"Cells let us walk, talk, think, make love and realize
  the bath water is cold." -- Lorraine Lee Cudmore
From: JP Massar
Subject: Re: What do CLisp and CormanLisp have in common?
Date: 
Message-ID: <3e714462.37443559@netnews.attbi.com>
On Thu, 13 Mar 2003 19:51:45 GMT, Kenny Tilton <·······@nyc.rr.com>
wrote:

>That's not a trick question. :)
>
>I just got the exact same bug (at runtime, but from a faulty assertion 
>generated during compilation of a defstruct accessor) out of compiled 
>Cells code under CLisp and CormanLisp.
>
>So...is one an offshoot of the other, or are both offshoots of something 
>else? Jes curious, but it also might help me work out a work-around.
>
 
It is certainly possible that they both make use of certain public
domain code to do the same things.

(I don't know that this is true, but it seems plausible)
From: Kenny Tilton
Subject: Re: What do CLisp and CormanLisp have in common?
Date: 
Message-ID: <3E71537E.4070303@nyc.rr.com>
JP Massar wrote:
> On Thu, 13 Mar 2003 19:51:45 GMT, Kenny Tilton <·······@nyc.rr.com>
> wrote:
> 
> 
>>That's not a trick question. :)
>>
>>I just got the exact same bug (at runtime, but from a faulty assertion 
>>generated during compilation of a defstruct accessor) out of compiled 
>>Cells code under CLisp and CormanLisp.
>>
>>So...is one an offshoot of the other, or are both offshoots of something 
>>else? Jes curious, but it also might help me work out a work-around.
>>
> 
>  
> It is certainly possible that they both make use of certain public
> domain code to do the same things.
> 
> (I don't know that this is true, but it seems plausible)
> 

Someone emailed me this smoking gun <g>:

> From the file places.lisp, Corman Lisp 2.01:
> 
> ;;;;    -------------------------------
> ;;;;    Copyright (c) 2001 Roger Corman
> ;;;;    All rights reserved.
> ;;;;    -------------------------------
> ;;;;
> ;;;;    File:           places.lisp
> ;;;;    Contents:       Corman Lisp SETF utilities.
> ;;;;                            Some of this code has been borrowed and adapted from
> ;;;;                CLISP.
> ;;;;    History:        10/10/2001  RGC  Created.
> ;;;;
> 

Check your Corman mail, bug (conc-name nil related) has been chased into 
a corner so you can stomp it (then send it along to CLisp). :)

-- 

  kenny tilton
  clinisys, inc
  http://www.tilton-technology.com/
  ---------------------------------------------------------------
"Cells let us walk, talk, think, make love and realize
  the bath water is cold." -- Lorraine Lee Cudmore
From: Martti Halminen
Subject: Re: Lisp help
Date: 
Message-ID: <3E70C620.9FD77021@kolumbus.fi>
Jame wrote:
> 
> Thank you very much for your information, but i just need to
> check the simple thing that is check whether the list
> contains only number then return sum of them, otherwise
> display error msg that this list is not valid. I am using
> mapcar function to check the number in the list however
> everytime mapcar apply on each element in the list it will
> show an errors message
> For example: (add '(1 2 v s d))
> --> list is not valid
>      list is not valid
>       list is not valid

While mapcar has its uses, this is not a place where I would use it. How
about

(defun add (list)
   (if (every #'numberp list) 
     (reduce #'+ list)
     (error "List ~A is not valid." list)))


or something in that style?

--
From: Kaz Kylheku
Subject: Re: Lisp help
Date: 
Message-ID: <cf333042.0303131107.52bd547c@posting.google.com>
Jame wrote in message news:<·················@cs.rmit.edu.au>...
> Any one show me the source code for mapcar function in Lisp

Download the sources to an open source Lisp implementation and take a
look.

> If i have a list L with some level such as "((1 3) (31
> (33))(32 43 23)) and i just one to get the first element of
> each level, how can i do it with recursive function instead
> of using
> (mapcar (first L)).

You mean (mapcar #'first L), of course.

Why would you want to do it recursively? That's a silly idea; if your
Lisp doesn't tail-optimize the recursion, you could end up blowing the
stack on very long lists. MAPCAR is the right idiom for doing this;
the requirement for any other way must be a student exercise.

To write the recursive solution, break the problem into two
subproblems: that of handling the first element of the list and the
rest of the list. Make sure you have a case for an empty or improper
list.

(defun take-first (list)
  (if (endp list)
    nil
    (cons (first (first list))
          (take-first (rest list)))))


> How to check whether the elements in the list is numeric or
> symbol.

Use the EVERY function to apply the INTEGERP or SYMBOLP predicate to
every element of the list, and compute the logical conjunction of the
results.

  (defun analyze-list-interactively (list)
    (if (every #'integerp list)
      (write-line "every element of the list is an integer")
      (write-line "not every element of the list is an integer"))
    (if (every #'symbolp list)
      (write-line "every element of the list is a symbol")
      (write-line "not every element of the list is a symbol"))
    (values))

What does it print when you pass in an empty list?