From: S. Robert James
Subject: Lisp Syntax - functions versus data
Date: 
Message-ID: <1172557054.945971.84460@j27g2000cwj.googlegroups.com>
I'm still struggling to master some of the finer points of Lisp
syntax.  Not the S-expressions, mind you, but the rules for forms
being either evaluated, referring to data, or referring to
functions...

I'm trying:
(defun mapcar. (func lst)
  (if (null lst)
      ()
      (append (list (funcall func (car lst))) (mapcar. func (cdr
lst)))))

(= '(3 4 5 6) (mapcar. '(lambda (x) (+ 3 x)) '(0 1 2 3)))

but can't get it to work... how should this be quoted?  Should I
switch to Scheme, which seems to have simpler syntax?

From: Joel Wilsson
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <1172570350.389182.82410@t69g2000cwt.googlegroups.com>
On Feb 27, 7:17 am, "S. Robert James" <············@gmail.com> wrote:
> (= '(3 4 5 6) (mapcar. '(lambda (x) (+ 3 x)) '(0 1 2 3)))

As in the first chapters of "Structure and Interpretation of Computer
Programs"[1], you can use the substitution rule to help you think
this through:

(mapcar. '(lambda (x) (+ 3 x)) '(0 1 2 3))
=> reduces to

(defun mapcar. ('(lambda (x) (+ 3 x)) '(0 1 2 3))
  (if (null '(0 1 2 3))
      ()
      (append (list (funcall '(lambda (x) (+ 3 x)) (car '(0 1 2 3))))
              (mapcar. '(lambda (x) (+ 3 x)) (cdr '(0 1 2 3))))))
=> condition for if is false, so it reduces to the second clause
of the if statement

(append (list (funcall '(lambda (x) (+ 3 x)) (car '(0 1 2 3)))
        (mapcar. '(lambda (x) (+ 3 x)) (cdr '(0 1 2 3)))
=> reducing the car and the cdr turns this into

(append (list (funcall '(lambda (x) (+ 3 x)) 0)
        (mapcar. '(lambda (x) (+ 3 x)) '(1 2 3)))
=> now replace the reader macro ' with the real thing, quote:

(append (list (funcall (quote (lambda (x) (+ 3 x))) 0)
        (mapcar. (quote (lambda (x) (+ 3 x))) (quote (1 2 3))))

Okay, now when you tried to run your example you got an error, but
you didn't tell us what the error was. SBCL says this:
The value (LAMBDA (X) (+ 3 X)) is not of type (OR FUNCTION SYMBOL).
   [Condition of type TYPE-ERROR]

So you have a problem with your lambda there.
Let's take a look at that:
(funcall (quote (lambda (x) (+ 3 x))) 0)

It seems you want to call a function that takes one argument and
increases it by 3. If you ask yourself "using lambda notation, how
would I write this function?" you'd answer "(lambda (x) (+ 3 x))"
Indeed, you've already said so above. So what's the quote there for?

What happens when you evaluate it? Ask the REPL:

CL-USER> (quote (lambda (x) (+ 3 x)))
(LAMBDA (X) (+ 3 X))

Perhaps you've already tried that (but with the ' reader macro) and
thought well, that's a lambda, that's what I want to apply to 0.
But here's the thing: it is NOT a lambda, it's a list!

The first element of the list is the symbol LAMBDA, but it's still a
list and you can't apply a list to something.
Compare the above with this:

CL-USER> (lambda (x) (+ 3 x))
#<FUNCTION (LAMBDA (X)) {4AFB010D}>

Now that gives you a function, and of course this function can be
applied to 0:

CL-USER> ((lambda (x) (+ 3 x)) 0)
3

Let's return to the beginning, this is what we tried to execute:
(= '(3 4 5 6) (mapcar. '(lambda (x) (+ 3 x)) '(0 1 2 3)))
Now you know the quoting of lambda is giving you a list where you
wanted a function. Don't quote the lambda and try again:

CL-USER> (mapcar. (lambda (x) (+ 3 x)) '(0 1 2 3))
(3 4 5 6)

So the above reduces to:
(= '(3 4 5 6) '(3 4 5 6))
Which blows up:
The value (3 4 5 6) is not of type NUMBER.
   [Condition of type TYPE-ERROR]

If you look at the syntax for = it's easy to see why:
(= number &rest more-numbers)
You want to compare lists here, not numbers.
You can use equal for that:
(equal '(3 4 5 6) '(3 4 5 6))

And so:

CL-USER> (equal '(3 4 5 6) (mapcar. (lambda (x) (+ 3 x)) '(0 1 2 3)))
T

[1]: http://mitpress.mit.edu/sicp/full-text/book/book.html
     http://www.swiss.ai.mit.edu/classes/6.001/abelson-sussman-lectures/
From: Alan Crowe
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <86odnfr9r5.fsf@cawtech.freeserve.co.uk>
"Joel Wilsson" <············@gmail.com> writes:
> It seems you want to call a function that takes one argument and
> increases it by 3. If you ask yourself "using lambda notation, how
> would I write this function?" you'd answer "(lambda (x) (+ 3 x))"
> Indeed, you've already said so above. So what's the quote there for?
> 
> What happens when you evaluate it? Ask the REPL:
> 
> CL-USER> (quote (lambda (x) (+ 3 x)))
> (LAMBDA (X) (+ 3 X))
> 
> Perhaps you've already tried that (but with the ' reader macro) and
> thought well, that's a lambda, that's what I want to apply to 0.
> But here's the thing: it is NOT a lambda, it's a list!
> 
> The first element of the list is the symbol LAMBDA, but it's still a
> list and you can't apply a list to something.
> Compare the above with this:

After I read this I thought: I bet I know what has confused
the original poster.

He has tried this

CL-USER> (mapcar (function +) '(1 2 3) '(1 1/2 1/3))
(2 5/2 10/3)

or

CL-USER> (mapcar #'+ '(1 2 3) '(1 1/2 1/3))
(2 5/2 10/3)

except no he hasn't, he's done this

CL-USER> (mapcar '+ '(1 2 3) '(1 1/2 1/3))
(2 5/2 10/3)

A newbie friendly mapcar would have said "No way man, `+' is
a symbol u gotta give me a function."

CL:MAPCAR actually says "I've been passed a symbol but I
need a function. I wonder if there is a function associated
with this symbol. I know, I'll look in the global function
namespace, if there is one there I will use. If not, I'll
give up and signal an error"

It is all explained in great detail in the hyperspec in
1.4.1.5 Designators.

The other must read page is

http://clhs.lisp.se/Body/v_pl_plp.htm

If you type + at the REPL so that it is treated as a
variable, not a function, it tells you the most recent form

CL-USER> +
(MAPCAR #'+ '(1 2 3) '(1 1/2 1/3))

Now + is the most recent form, so the value of the variable
+ is + and this works

CL-USER> (mapcar + '(1 2 3) '(1 1/2 1/3))
(2 5/2 10/3)

But only once, because the value of the variable + keeps
changing as you type in new forms

CL-USER> (mapcar + '(1 2 3) '(1 1/2 1/3))

Type-error in KERNEL::OBJECT-NOT-TYPE-ERROR-HANDLER:
   (MAPCAR + '(1 2 3) '(1 1/2 1/3)) is not of type (OR FUNCTION SYMBOL)
   [Condition of type TYPE-ERROR]

Ponder the type that MAPCAR said it wanted for its function
argument: (OR FUNCTION SYMBOL)

Alan Crowe
Edinburgh
Scotland
From: =?ISO-8859-15?Q?Andr=E9_Thieme?=
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <es1dvc$duj$1@registered.motzarella.org>
Alan Crowe schrieb:
> "Joel Wilsson" <············@gmail.com> writes:
>> It seems you want to call a function that takes one argument and
>> increases it by 3. If you ask yourself "using lambda notation, how
>> would I write this function?" you'd answer "(lambda (x) (+ 3 x))"
>> Indeed, you've already said so above. So what's the quote there for?
>>
>> What happens when you evaluate it? Ask the REPL:
>>
>> CL-USER> (quote (lambda (x) (+ 3 x)))
>> (LAMBDA (X) (+ 3 X))
>>
>> Perhaps you've already tried that (but with the ' reader macro) and
>> thought well, that's a lambda, that's what I want to apply to 0.
>> But here's the thing: it is NOT a lambda, it's a list!
>>
>> The first element of the list is the symbol LAMBDA, but it's still a
>> list and you can't apply a list to something.
>> Compare the above with this:
> 
> After I read this I thought: I bet I know what has confused
> the original poster.
> 
> He has tried this
> 
> CL-USER> (mapcar (function +) '(1 2 3) '(1 1/2 1/3))
> (2 5/2 10/3)
> 
> or
> 
> CL-USER> (mapcar #'+ '(1 2 3) '(1 1/2 1/3))
> (2 5/2 10/3)
> 
> except no he hasn't, he's done this
> 
> CL-USER> (mapcar '+ '(1 2 3) '(1 1/2 1/3))
> (2 5/2 10/3)
> 
> A newbie friendly mapcar would have said "No way man, `+' is
> a symbol u gotta give me a function."

Yes, good point.
Also a bunch of functions that take a test function accept things like:
'eq  or  'equal
Strange.
Why don't they go so far and take strings too?
(make-hash-table :test "equal")


Andr�
-- 
From: Tim Bradshaw
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <1172588487.345999.303080@j27g2000cwj.googlegroups.com>
On Feb 27, 2:07 pm, André Thieme <address.good.until.
···········@justmail.de> wrote:

> Why don't they go so far and take strings too?
> (make-hash-table :test "equal")

Because it's in the wrong case (assuming standard CL), and even if it
was in the right case how would the system know which package you
meant?  You'd need some kind of thing which was a bit like a string
but had package information and so on, and ideally could arrange for
the slow bits of the lookup to happen as the code was read rather than
at runtime.  Now, what was the name of that type again?

--tim
From: S. Robert James
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <1172590633.198435.36710@h3g2000cwc.googlegroups.com>
Thanks for all the great explanations.

One thing that I still don't have clear is: what is the difference
between
(lambda (x) (+ 3 x))
and
 #'(lambda (x) (+ 3 x))
From: Drew Crampsie
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <87vehnikrf.fsf@osiris.tech.coop>
"S. Robert James" <············@gmail.com> writes:

> Thanks for all the great explanations.
>
> One thing that I still don't have clear is: what is the difference
> between
> (lambda (x) (+ 3 x))
> and
>  #'(lambda (x) (+ 3 x))


The former is a macro which expands into the latter. The latter
includes the #' _reader macro_ that expands to (function (lambda (x)
(+ 3 x))). FUNCTION is a special form. Once you've grokked how this
can work when it seems 'obvious' that the second form recursively 
expands itself, you'll understand a great bit more about CL's 
evaluation model.

This is also one of the most FAQs in c.l.l, so try the archives next
time first. :) 

Cheers, 

drewc

-- 
Posted via a free Usenet account from http://www.teranews.com
From: =?ISO-8859-15?Q?Andr=E9_Thieme?=
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <es1ktb$n68$1@registered.motzarella.org>
S. Robert James schrieb:
> Thanks for all the great explanations.
> 
> One thing that I still don't have clear is: what is the difference
> between
> (lambda (x) (+ 3 x))
> and
>  #'(lambda (x) (+ 3 x))

The thing is, when you write:
#'(lambda ...)  it is the same as (function (lambda ...)).
In that case the special operator FUNCTION knows how to handle this
its argument - a list.

When you do just (lambda ...) then it calls a macro that has the
name LAMBDA. This macro now returns (function (lambda ...)) and then
evals/compiles that instead.

Now you have two choices: write #' or don't. It is a style question.
If you decided what you prefer more just use it always.


Andr�
-- 
From: Zach Beane
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <m3vehnlgle.fsf@unnamed.xach.com>
André Thieme <······························@justmail.de> writes:

> S. Robert James schrieb:
> > Thanks for all the great explanations.
> > One thing that I still don't have clear is: what is the difference
> > between
> > (lambda (x) (+ 3 x))
> > and
> >  #'(lambda (x) (+ 3 x))
> 
> The thing is, when you write:
> #'(lambda ...)  it is the same as (function (lambda ...)).
> In that case the special operator FUNCTION knows how to handle this
> its argument - a list.
> 
> When you do just (lambda ...) then it calls a macro that has the
> name LAMBDA. This macro now returns (function (lambda ...)) and then
> evals/compiles that instead.
> 
> Now you have two choices: write #' or don't. It is a style question.
> If you decided what you prefer more just use it always.

There is one exception. The evaluator has a special rule for a form
that begins with a lambda form:

   ((lambda (x) (+ x 5)) 10) => 15

It does not have any such rule for a function form:

   ((function (lambda (x) (+ x 5))) 10) => illegal function call error

I can't think of any reason, these days, to take advantage of the
former.

Zach
From: drewc
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <87r6sbik7n.fsf@osiris.tech.coop>
Zach Beane <····@xach.com> writes:

> There is one exception. The evaluator has a special rule for a form
> that begins with a lambda form:
>
>    ((lambda (x) (+ x 5)) 10) => 15
>
> It does not have any such rule for a function form:
>
>    ((function (lambda (x) (+ x 5))) 10) => illegal function call error
>
> I can't think of any reason, these days, to take advantage of the
> former.

I've found it really 'nice' to be able to use a lambda in place of a
function name (symbol) when generating code. It has, in certain cases,
made macros easier to write and maintain without having to special
case something.

Having said that, adding FUNCALL would not have been all that bad, and
if they were to remove this feature from the mythical future CL
standard, i would not miss it all that much.

Cheers, 

drewc

>
> Zach

-- 
Posted via a free Usenet account from http://www.teranews.com
From: Vassil Nikolov
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <yy8v8xejm1um.fsf@eskimo.com>
On 27 Feb 2007 11:18:05 -0500, Zach Beane <····@xach.com> said:
| ...
| There is one exception. The evaluator has a special rule for a form
| that begins with a lambda form:

|    ((lambda (x) (+ x 5)) 10) => 15

| It does not have any such rule for a function form:

|    ((function (lambda (x) (+ x 5))) 10) => illegal function call error

| I can't think of any reason, these days, to take advantage of the
| former.

  In addition to what others wrote, lambda forms are useful as intermediate
  steps when abstracting away an operation from what started as an
  expression.

  Also, when executing the same piece of code multiple times with
  different values for parameters, it is easier to replace the values
  if they come at the end, i.e. it is easier (takes fewer keystrokes) to
  edit

    ((lambda (x y) ... x ... y ...) foo bar)

  to produce

    ((lambda (x y) ... x ... y ...) baz quux)

  rather than

    (let ((x foo) (y bar)) ... x ... y ...)

  to produce

    (let ((x baz) (y quux)) ... x ... y ...)

  ---Vassil.


-- 
mind mate, n.
  One of two persons mentally compatible with each other (cf. soul mate).
From: Timofei Shatrov
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <45e4aec2.54805015@news.readfreenews.net>
On 27 Feb 2007 11:18:05 -0500, Zach Beane <····@xach.com> tried to confuse
everyone with this message:

>There is one exception. The evaluator has a special rule for a form
>that begins with a lambda form:
>
>   ((lambda (x) (+ x 5)) 10) => 15
>
>It does not have any such rule for a function form:
>
>   ((function (lambda (x) (+ x 5))) 10) => illegal function call error
>
>I can't think of any reason, these days, to take advantage of the
>former.

This is useful for writing quines!

-- 
|Don't believe this - you're not worthless              ,gr---------.ru
|It's us against millions and we can't take them all... |  ue     il   |
|But we can take them on!                               |     @ma      |
|                       (A Wilhelm Scream - The Rip)    |______________|
From: Duane Rettig
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <o0649nlfkn.fsf@gemini.franz.com>
"Tim Bradshaw" <··········@tfeb.org> writes:

> On Feb 27, 2:07 pm, Andr� Thieme <address.good.until.
> ···········@justmail.de> wrote:
>
>> Why don't they go so far and take strings too?
>> (make-hash-table :test "equal")
>
> Because it's in the wrong case (assuming standard CL), and even if it
> was in the right case how would the system know which package you
> meant?  You'd need some kind of thing which was a bit like a string
> but had package information and so on, and ideally could arrange for
> the slow bits of the lookup to happen as the code was read rather than
> at runtime.  Now, what was the name of that type again?

It's even worse than that: whether the case is wrong or not depends
entirely on whether READ is used to convert the symbol to its obvious
(in this situation) corresponding function name; if read were being
used, and the readtable-case at the time were :upcase or :invert, then
(along with your notes about how the package is discovered) the symbol
could have been found.  Of course, the question is: is it worth
bringing the reader into this otherwise straightforward situation, to
add ambiguity to an otherwise perfectly specifiable and efficient
functionality?  (I think not)

Also, as far as the question of why 'equal is accepted for #'equal:
they both provide service to the programmer, in that both identify
functions, but the semantics are slightly different as to whether the
function is global or local in its meaning; given a function named bar
and a function foo with an flet of bar within it, then within foo any
reference to 'bar names the global definition, whereas #'bar names the
flet.  This is not the same as trying to name a function using "bar",
because there is no ambiguity in either of the first two naming
forms, and that's why they're standard and the string version isn't.

-- 
Duane Rettig    ·····@franz.com    Franz Inc.  http://www.franz.com/
555 12th St., Suite 1450               http://www.555citycenter.com/
Oakland, Ca. 94607        Phone: (510) 452-2000; Fax: (510) 452-0182   
From: Tim Bradshaw
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <es0ng6$dn3$1$830fa7a5@news.demon.co.uk>
On 2007-02-27 06:17:34 +0000, "S. Robert James" <············@gmail.com> said:

> (= '(3 4 5 6) (mapcar. '(lambda (x) (+ 3 x)) '(0 1 2 3)))

Here you have passed the literal list (LAMBDA (X) (+ 3 X)) as a 
function, because you quoted that form: Instead you need to pass a 
function.  In CL the way to get this is by using the FUNCTION operator: 
(FUNCTION (LAMBDA ...)), which is conveniently abbreviated as #'(LAMBDA 
...).  In fact LAMBDA itself is (now) defined as a macro to produce 
this, so you can say merely (LAMBDA ...) (without the quote!).

> 
> but can't get it to work... how should this be quoted?  Should I
> switch to Scheme, which seems to have simpler syntax?

If you value that above what CL has to offer, then perhaps.
From: André Thieme
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <es14it$1qe$1@registered.motzarella.org>
Hello Tim! Maybe I misread this sentence:

> Here you have passed the literal list (LAMBDA (X) (+ 3 X)) as a 
> function, because you quoted that form: Instead you need to pass a 
> function.

What does it mean?
Instead of passing a function he should have passed a... function?


Andr�
-- 
From: Tim Bradshaw
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <1172580512.288659.131360@t69g2000cwt.googlegroups.com>
On Feb 27, 11:27 am, André Thieme
> > Here you have passed the literal list (LAMBDA (X) (+ 3 X)) as a
> > function, because you quoted that form: Instead you need to pass a
> > function.
>
> What does it mean?
> Instead of passing a function he should have passed a... function?

Sorry! What I meant to say was: he passed a (literal) list as an
argument to a function which was expecting that argument to be a
function.

It was early in the morning, and the cat wrote it anyway.

--tim
From: Rainer Joswig
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <joswig-BD6795.12491627022007@news-europe.giganews.com>
In article <············@registered.motzarella.org>,
 Andr� Thieme <······························@justmail.de> wrote:

> Hello Tim! Maybe I misread this sentence:
> 
> > Here you have passed the literal list (LAMBDA (X) (+ 3 X)) as a 
> > function, because you quoted that form: Instead you need to pass a 
> > function.
> 
> What does it mean?
> Instead of passing a function he should have passed a... function?
> 
> 
> Andr�
> -- 


'(LAMBDA (X) (+ 3 X))  is a list

#'(LAMBDA (X) (+ 3 X))  is a function
From: Pascal Bourguignon
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <87r6s33khx.fsf@voyager.informatimago.com>
Rainer Joswig <······@lisp.de> writes:

> In article <············@registered.motzarella.org>,
>  Andr� Thieme <······························@justmail.de> wrote:
>
>> Hello Tim! Maybe I misread this sentence:
>> 
>> > Here you have passed the literal list (LAMBDA (X) (+ 3 X)) as a 
>> > function, because you quoted that form: Instead you need to pass a 
>> > function.
>> 
>> What does it mean?
>> Instead of passing a function he should have passed a... function?
>> 
>> 
>> Andr�
>> -- 
>
>
> '(LAMBDA (X) (+ 3 X))  is a list
>
> #'(LAMBDA (X) (+ 3 X))  is a function

To make it more obvious to newbies, let's write it as:

(QUOTE    (lambda (x) (+ 3 x))) is a list     (data),
(FUNCTION (lambda (x) (+ 3 x))) is a function (code).


Down with ' and #' !   ;-)

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
        Un chat errant
se soulage
        dans le jardin d'hiver
                                        Shiki
From: Vassil Nikolov
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <yy8vhcszugl2.fsf@eskimo.com>
On Mon, 05 Mar 2007 14:09:14 +0100, Pascal Bourguignon <···@informatimago.com> said:
| To make it more obvious to newbies, let's write it as:

| (QUOTE    (lambda (x) (+ 3 x))) is a list     (data),
| (FUNCTION (lambda (x) (+ 3 x))) is a function (code).

| Down with ' and #' !   ;-)

  And you can get from A to B with

    (coerce (quote (lambda (x) (+ 3 x))) (quote function))

  but it is a one-way trip.  (It will also be obvious, of course,
  that QUOTE means the same in all its three occurrences, unlike
  FUNCTION in its two occurrences...)

  ---Vassil.


-- 
Is your code free of side defects?
From: Rainer Joswig
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <joswig-66F40D.08503927022007@news-europe.giganews.com>
In article <·······················@j27g2000cwj.googlegroups.com>,
 "S. Robert James" <············@gmail.com> wrote:

> I'm still struggling to master some of the finer points of Lisp
> syntax.  Not the S-expressions, mind you, but the rules for forms
> being either evaluated, referring to data, or referring to
> functions...
> 
> I'm trying:
> (defun mapcar. (func lst)
>   (if (null lst)
>       ()
>       (append (list (funcall func (car lst))) (mapcar. func (cdr
> lst)))))
> 
> (= '(3 4 5 6) (mapcar. '(lambda (x) (+ 3 x)) '(0 1 2 3)))
> 
> but can't get it to work... how should this be quoted?  Should I
> switch to Scheme, which seems to have simpler syntax?

What kind of error do you get?

I see:

(LAMBDA (X)
  (+ 3 X)) is not of type (OR SYMBOL FUNCTION), and can't be FUNCALLed 
or APPLYed

A function needs ro be quoted by #' .

So:

(= '(3 4 5 6) (mapcar. #'(lambda (x) (+ 3 x)) '(0 1 2 3)))

Then you get:

value (3 4 5 6) is not of the expected type NUMBER.

Because = is only for comparing numbers.

See:
http://www.lisp.org/HyperSpec/Body/fun_eqcm_sleq__lteqcm_gteq.html

So:

(equal '(3 4 5 6) (mapcar. #'(lambda (x) (+ 3 x)) '(0 1 2 3)))


Also:

Try to write MAPCAR. without APPEND. Using APPEND is often inefficient.
From: Thomas A. Russ
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <ymiwt2375zn.fsf@sevak.isi.edu>
"S. Robert James" <············@gmail.com> writes:

> I'm still struggling to master some of the finer points of Lisp
> syntax.  Not the S-expressions, mind you, but the rules for forms
> being either evaluated, referring to data, or referring to
> functions...
> 
> I'm trying:
> (defun mapcar. (func lst)
>   (if (null lst)
>       ()
>       (append (list (funcall func (car lst))) (mapcar. func (cdr
> lst)))))
> 
> (= '(3 4 5 6) (mapcar. '(lambda (x) (+ 3 x)) '(0 1 2 3)))
> 
> but can't get it to work... how should this be quoted?  Should I
> switch to Scheme, which seems to have simpler syntax?

Um, what fails?
I just pasted your code into my lisp and it (generally) worked.

What results do you get from 

  (mapcar. '(lambda (x) (+ 3 x)) '(0 1 2 3))

The only problem I see is that the "=" function in Lisp is only defined
to work on numbers.  Try using EQUAL instead.


-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: André Thieme
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <es14u4$2su$1@registered.motzarella.org>
S. Robert James schrieb:
> I'm still struggling to master some of the finer points of Lisp
> syntax.  Not the S-expressions, mind you, but the rules for forms
> being either evaluated, referring to data, or referring to
> functions...
> 
> I'm trying:
> (defun mapcar. (func lst)
>   (if (null lst)
>       ()
>       (append (list (funcall func (car lst))) (mapcar. func (cdr
> lst)))))
> 
> (= '(3 4 5 6) (mapcar. '(lambda (x) (+ 3 x)) '(0 1 2 3)))
> 
> but can't get it to work... how should this be quoted?  Should I
> switch to Scheme, which seems to have simpler syntax?

No need to switch to Scheme.
In Common Lisp you can say:
"(defun mapcar. (function list)".

Another suggestion:
instead of writing
(if (null list)
     ()
     (code))

say:

(when list
   (code))

An IF that returns an empty list can be replaced with either
WHEN or with UNLESS.


Andr�
-- 
From: Vassil Nikolov
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <yy8vabyzm287.fsf@eskimo.com>
On Tue, 27 Feb 2007 12:33:22 +0100, =?ISO-8859-1?Q?Andr=E9_Thieme?= <······························@justmail.de> said:
| ...
| Another suggestion:
| instead of writing
| (if (null list)
|      ()
|      (code))

| say:

| (when list
|    (code))

| An IF that returns an empty list can be replaced with either
| WHEN or with UNLESS.

  The usual approach is to use WHEN or UNLESS when evaluating for side
  effect; if the returned value will be used, IF is in order with both a
  "then" and an "else" expression.

  ---Vassil.


-- 
mind mate, n.
  One of two persons mentally compatible with each other (cf. soul mate).
From: Rob Warnock
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <acmdnVoomp44mnjYnZ2dnUVZ_hGdnZ2d@speakeasy.net>
Vassil Nikolov  <···············@pobox.com> wrote:
+---------------
| Andre Thieme <······························@justmail.de> said:
| | say:
| | (when list
| |    (code))
| | An IF that returns an empty list can be replaced with either
| | WHEN or with UNLESS.
| 
|   The usual approach is to use WHEN or UNLESS when evaluating for side
|   effect; if the returned value will be used, IF is in order with both
|   a "then" and an "else" expression.
+---------------

Yes, but... even that rule has its exceptions. After all, the value
of WHEN when the test is false *is* well-defined in the CLHS to be NIL,
and I find that (for example) doing conditional splicing of optional
forms into backquoted macro bodies looks better done this way:

    ,@(when test (list form1 form2 ...))

or even this way if there's just one form
[though I still personally prefer using the WHEN]:

    ,@(and test (list form))

rather than this way:

    ,@(if test (list form1 form2 ...) nil)

YMMV, of course.


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Zach Beane
Subject: Re: Lisp Syntax - functions versus data
Date: 
Message-ID: <m3k5y2l9fo.fsf@unnamed.xach.com>
Vassil Nikolov <···············@pobox.com> writes:

>   The usual approach is to use WHEN or UNLESS when evaluating for side
>   effect; if the returned value will be used, IF is in order with both a
>   "then" and an "else" expression.

I often start off predicates with something like this:

   (defun foostringp (string)
     (when (and (stringp string) 
                (= (length string) 20))
       ...))

Zach