From: Miss Elaine Eos
Subject: Another n00b Q: lambda functions
Date: 
Message-ID: <Misc-D096EC.19295626112006@coml.de>
I really like lisp's anonymous (lambda) functions, and get the concept, 
entirely.  What I *don't* get is the syntax.  I tried to defun something 
that would apply my function (foo) to every object on a list (lst), and 
came up with:

(defun fn-all (foo lst)
   (if (functionp foo)
      (dolist (obj lst)
         (apply #'foo (list obj))
      )
      nil
   ))

but, when I do:

> (setf abc (list 'a 'b 'c))
> (fn-all (lambda (x) (format t "~A~%" x)) abc)

I get 

> Error in process listener(1): Undefined function FOO called with arguments (A) .
> While executing: FN-ALL

What adjustments do I want to make?  (Besides my closing parens, I mean 
-- I need them there for now; that's my "training wheels" :)

-- 
Please take off your pants or I won't read your e-mail.
I will not, no matter how "good" the deal, patronise any business which sends
unsolicited commercial e-mail or that advertises in discussion newsgroups.

From: ······@gmail.com
Subject: Re: Another n00b Q: lambda functions
Date: 
Message-ID: <1164599086.883604.282190@n67g2000cwd.googlegroups.com>
> I tried to defun something
> that would apply my function (foo) to every object on a list (lst),

First off, there's already several ways to do this:

(MAPCAR (LAMBDA (X) (REVERSE X)) '((A B) (C D) (E F)))
=> ((B A) (D C) (F E))

or:

(LOOP FOR ITEM IN '((A B) (C D) (E F))
   COLLECT (FUNCALL (LAMBDA (X) (REVERSE X)) ITEM))
=> ((B A) (D C) (F E))

Now, in your case you have a couple of simple problems:

> (defun fn-all (foo lst)
>    (if (functionp foo)
>       (dolist (obj lst)
>          (apply #'foo (list obj))
>       )
>       nil
>    ))
>
> but, when I do:
>
> > (setf abc (list 'a 'b 'c))
> > (fn-all (lambda (x) (format t "~A~%" x)) abc)
>
> I get
>
> > Error in process listener(1): Undefined function FOO called with arguments (A) .
> > While executing: FN-ALL

The most obvious problem is that the #' syntax tries to find the
function called FOO, not the function bound to the variable FOO. So,
this will do what you intended (simplified to remove the irrelevant
functionp test):

(defun fn-all (foo lst)
    (dolist (obj lst)
         (apply foo (list obj))))

(fn-all (lambda (x) (format t "~A~%" x)) '(a b c))
=>
A
B
C
NIL

Note, however that you can get even simpler as:

(defun fn-all (foo lst)
    (dolist (obj lst)
         (funcall foo obj)))

With funcall you don't have to put the args to the function into a
list.

Also, you really don't want to be using dolist as it doesn't return a
result (in the form you're using it). Watch:

(fn-all (lambda (x) (reverse x)) '((a b) (c d) (e f)))
=> nil

Better would be:

(defun fn-all (foo lst)
    (mapcar foo lst))

(FN-ALL (LAMBDA (X) (REVERSE X)) '((A B) (C D) (E F)))
((B A) (D C) (F E))

But, of course, this trivially reduces to mapcar!

Rrrrrrrrroooooooowwwwwwww! :-)
From: Miss Elaine Eos
Subject: Re: Another n00b Q: lambda functions
Date: 
Message-ID: <Misc-3AEC20.20331526112006@coml.de>
In article <························@n67g2000cwd.googlegroups.com>,
 ······@gmail.com wrote:

> > I tried to defun something
> > that would apply my function (foo) to every object on a list (lst),

> First off, there's already several ways to do this:
   [lots of good stuff, snip]

Ok, I haven't gotten to map yet in the book (still on chapter 2!)  One 
of the reasons I'm doing it this way is to have in my notes some quick 
sample hacks that demonstrate the various functions.  Do efficiency 
isn't key, now -- I'm still trying to learn syntax.

But thanks!  :)

-- 
Please take off your pants or I won't read your e-mail.
I will not, no matter how "good" the deal, patronise any business which sends
unsolicited commercial e-mail or that advertises in discussion newsgroups.
From: ······@gmail.com
Subject: Re: Another n00b Q: lambda functions
Date: 
Message-ID: <1164607887.688410.13160@45g2000cws.googlegroups.com>
> Ok, I haven't gotten to map yet in the book (still on chapter 2!)  One
> of the reasons I'm doing it this way is to have in my notes some quick
> sample hacks that demonstrate the various functions.  Do efficiency
> isn't key, now -- I'm still trying to learn syntax.

Ahh, well, if you're only on Chapter 2, y'ought'n't be written dolists
a'tall, but rather hackn it the hard way:

  (defun fn-all (fn list)
    (cond ((null list) nil)
             (t (cons (funcall fn (car list))
                         (fn-all fn (cdr list))))
      ))

Or, even better'n that:

  (defun fn-all (fn list)
    (cond ((null list) nil)
             ((not (consp list))
              (funcall fn list))
             (t (cons (fn-all fn (car list))
                         (fn-all fn (cdr list))))
      ))

(FN-ALL (LAMBDA (LET)
                 (PRINT LET)
                 (FORMAT NIL "[~a]" LET))
           '((A (B (C D) (E F (G H I)) ((J K (L) M) N O (P Q)) R) S (T
(U V))) W (X (Y Z))))

A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
(("[A]"
  ("[B]" ("[C]" "[D]") ("[E]" "[F]" ("[G]" "[H]" "[I]")) (("[J]" "[K]"
# "[M]") "[N]" "[O]" ("[P]" "[Q]")) "[R]")
  "[S]" ("[T]" ("[U]" "[V]")))
 "[W]" ("[X]" ("[Y]" "[Z]")))

Or better yet:

  (defun fn-all (fn list)
    (cond ((null list) nil)
             ((not (consp list))
              (list (funcall fn list)))
             (t (append (fn-all fn (car list))
                            (fn-all fn (cdr list))))
      ))

(FN-ALL (LAMBDA (LET)
                   (PRINT LET)
                   (FORMAT NIL "[~a]" LET))
             '((A (B (C D) (E F (G H I)) ((J K (L) M) N O (P Q)) R) S
(T (U V))) W (X (Y Z))))
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
("[A]" "[B]" "[C]" "[D]" "[E]" "[F]" "[G]" "[H]" "[I]" "[J]" "[K]"
"[L]" "[M]" "[N]" "[O]" "[P]" "[Q]" "[R]" "[S]"
 "[T]" "[U]" "[V]" "[W]" "[X]" "[Y]" "[Z]")

Ain't Lisp just the cat's paw! ;-)

-- Tig
From: Miss Elaine Eos
Subject: Re: Another n00b Q: lambda functions
Date: 
Message-ID: <Misc-7EA7EC.20400027112006@coml.de>
In article <·······················@45g2000cws.googlegroups.com>,
 ······@gmail.com wrote:

> Ahh, well, if you're only on Chapter 2, y'ought'n't be written dolists
> a'tall, but rather hackn it the hard way:

Hey, I do not second-guess Paul Graham (who went & put dolist right 
there on p24!)  I figure, if the man's going to go through all that 
trouble to learn me lisp, at least I can do is try to learn something!

Btw, when I 1st started this endeavor, I was disappointed that most of 
the on-line tutorials stuck to doling out syntax, with little 
explanation of concepts.  I bought _ANSI Common Lisp_ on someone's 
recommendation (hey, wait -- it may have been PG's! Hmmm...) that it 
actually introduced this "new way of thinking about programming" that 
I'd heard about (yes, yes -- it's really the OLD way of thinking, but 
you know what I mean!)

Anyway, he does a very nice job.  It's not exactly "Lisp for C 
programmers", but it's still an excellent book, and I highly recommend 
it for anyone just starting lisp.

Misc "An unsolicited endorsement...."

-- 
Please take off your pants or I won't read your e-mail.
I will not, no matter how "good" the deal, patronise any business which sends
unsolicited commercial e-mail or that advertises in discussion newsgroups.
From: Robert Uhl
Subject: Re: Another n00b Q: lambda functions
Date: 
Message-ID: <m37ixg6km9.fsf@latakia.dyndns.org>
Miss Elaine Eos <····@*your-pants*PlayNaked.com> writes:
>
> Anyway, he does a very nice job.  It's not exactly "Lisp for C
> programmers", but it's still an excellent book, and I highly recommend
> it for anyone just starting lisp.

Also take a look at Practical Common Lisp
<http://www.gigamonkeys.com/book>.  It's an incredibly introduction to CL.

-- 
Robert Uhl <http://public.xdi.org/=ruhl>
I owe the government $3,400 in taxes.  So I sent them two hammers and a
toilet seat.                                          --Michael McShane
From: Ken Tilton
Subject: Re: Another n00b Q: lambda functions
Date: 
Message-ID: <zoRah.212$Gs5.167@newsfe10.lga>
Miss Elaine Eos wrote:
> In article <·······················@45g2000cws.googlegroups.com>,
>  ······@gmail.com wrote:
> 
> 
>>Ahh, well, if you're only on Chapter 2, y'ought'n't be written dolists
>>a'tall, but rather hackn it the hard way:
> 
> 
> Hey, I do not second-guess Paul Graham (who went & put dolist right 
> there on p24!) 

...and who does not like Common Lisp (overheard at ILC 200x, and fer 
chrissakes he is trying to create a whole new language to replace it). 
His cheerleading is great, but when he gets down into the syntax he 
starts sounding like Emperor Joe Jr: "cond has too many parentheses" 
(overheard at ILC 200x).

Just Learn Loop. PG won't teach you, try PCL. dolist has too many 
parentheses.

kt

-- 
Cells: http://common-lisp.net/project/cells/

"I'll say I'm losing my grip, and it feels terrific."
    -- Smiling husband to scowling wife, New Yorker cartoon
From: ········@gmail.com
Subject: Re: Another n00b Q: lambda functions
Date: 
Message-ID: <1164698965.636253.173030@j72g2000cwa.googlegroups.com>
Ken Tilton wrote:
> Just Learn Loop. PG won't teach you, try PCL. dolist has too many
> parentheses.

I agree... Hey, I have an idea; why don't we redeisgn Lisp's basic
syntax so that it doesn't use so many parentheses!

[Before the humour impaired among us jump down my throat ... in case it
isn't obvious: I'M KIDDING!]
From: Harald Hanche-Olsen
Subject: Re: Another n00b Q: lambda functions
Date: 
Message-ID: <pco3b83rg1q.fsf@shuttle.math.ntnu.no>
+ ·········@gmail.com" <········@gmail.com>:

| [Before the humour impaired among us jump down my throat ... in case it
| isn't obvious: I'M KIDDING!]

Spoilsport.

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- It is undesirable to believe a proposition
  when there is no ground whatsoever for supposing it is true.
  -- Bertrand Russell
From: Wolfram Fenske
Subject: Re: Another n00b Q: lambda functions
Date: 
Message-ID: <1164704321.353255.104570@80g2000cwy.googlegroups.com>
Ken Tilton <·········@gmail.com> writes:

> Miss Elaine Eos wrote:
>> In article <·······················@45g2000cws.googlegroups.com>,
>>  ······@gmail.com wrote:
>>
>>>Ahh, well, if you're only on Chapter 2, y'ought'n't be written dolists
>>>a'tall, but rather hackn it the hard way:
>> Hey, I do not second-guess Paul Graham (who went & put dolist right
>> there on p24!)
>
> ...and who does not like Common Lisp (overheard at ILC 200x, and fer
> chrissakes he is trying to create a whole new language to replace
> it). His cheerleading is great, but when he gets down into the syntax
> he starts sounding like Emperor Joe Jr: "cond has too many
> parentheses" (overheard at ILC 200x).

And let's not forget: PG uses *vi*, not Emacs [1].  He is obviously
evil. ;-)


Footnotes:
[1]  <http://reddit.com/info/21918/comments>

--
Wolfram Fenske

A: Yes.
>Q: Are you sure?
>>A: Because it reverses the logical flow of conversation.
>>>Q: Why is top posting frowned upon?
From: Wolfram Fenske
Subject: Re: Another n00b Q: lambda functions
Date: 
Message-ID: <1164704659.411860.25660@l12g2000cwl.googlegroups.com>
Ken Tilton <·········@gmail.com> writes:

> Miss Elaine Eos wrote:
>> In article <·······················@45g2000cws.googlegroups.com>,
>>  ······@gmail.com wrote:
>>
>>>Ahh, well, if you're only on Chapter 2, y'ought'n't be written dolists
>>>a'tall, but rather hackn it the hard way:
>> Hey, I do not second-guess Paul Graham (who went & put dolist right
>> there on p24!)
>
> ...and who does not like Common Lisp (overheard at ILC 200x, and fer
> chrissakes he is trying to create a whole new language to replace
> it). His cheerleading is great, but when he gets down into the syntax
> he starts sounding like Emperor Joe Jr: "cond has too many
> parentheses" (overheard at ILC 200x).

And let's not forget: PG uses *vi*, not Emacs [1].  He is obviously
evil. ;-)


Footnotes:
[1]  <http://reddit.com/info/21918/comments>

--
Wolfram Fenske

A: Yes.
>Q: Are you sure?
>>A: Because it reverses the logical flow of conversation.
>>>Q: Why is top posting frowned upon?
From: Bill Atkins
Subject: Re: Another n00b Q: lambda functions
Date: 
Message-ID: <1164719051.654659.156300@j44g2000cwa.googlegroups.com>
Newsgroups: comp.lang.lisp
Subject: Re: Another n00b Q: lambda functions
X-Draft-From: ("comp.lang.lisp" 89075)
References: <··························@coml.de>
<························@n67g2000cwd.googlegroups.com>
<··························@coml.de>
<·······················@45g2000cws.googlegroups.com>
<··························@coml.de> <·················@newsfe10.lga>
From: Bill Atkins <······@rpi.edu>
--text follows this line--
Ken Tilton <·········@gmail.com> writes:

> Just Learn Loop. PG won't teach you, try PCL. dolist has too many
> parentheses.

For serious.  Why do you people even bother with Graham's books?
Where are you hearing about his books and why aren't you hearing about
PCL there? (if the answer is paulgraham.com, then I guess that
explains it pretty nicely)

But look at the fine craftmanship of P. Seibel:

(define-tagged-binary-class id3-tag ()
  ((identifier     (iso-8859-1-string :length 3))
   (major-version  u1)
   (revision       u1)
   (flags          u1)
   (size           id3-tag-size))
  (:dispatch
   (ecase major-version
     (2 'id3v2.2-tag)
     (3 'id3v2.3-tag))))

This uses his custom define-tagged-binary-class macro to create a
class to represent an ID3 tag in an MP3 file and then generates
methods to read or write the object back to a stream, in the order
given.  It even lets you dispatch to other classes to continue reading
or writing the file.

This is really neat code.  You'll have Ah-ha moments every page of
PCL.  The same goes for PAIP.

Now compare this to what you get with Graham:

(defun compr (elt n lst)
  (if (null lst)
      (list (n-elts elt n))
      (let ((next (car lst)))
        (if (eql next elt)
            (compr elt (+ n 1) (cdr lst))
            (cons (n-elts elt n)
                  (compr next 1 (cdr lst)))))))

(defun finder (obj vec start end)
  (let ((range (- end start)))
    (if (zerop range)
        (if (eql obj (aref vec start))
            obj
            nil)
        (let ((mid (+ start (round (/ range 2)))))
          (let ((obj2 (aref vec mid)))
            (if (< obj obj2)
                (finder obj vec start (- mid 1))
                (if (> obj obj2)
                    (finder obj vec (+ mid 1) end)
                    obj)))))))

(defun num-year (n)
  (if (< n 0)
      (do* ((y (- yzero 1) (- y 1))
            (d (- (year-days y)) (- d (year-days y))))
           ((<= d n) (values y (- n d))))
      (do* ((y yzero (+ y 1))
            (prev 0 d)
            (d (year-days y) (+ d (year-days y))))
           ((> d n) (values y (- n prev))))))

(defun first-hit (pt xr yr zr)
  (let (surface hit dist)
    (dolist (s *world*)
      (let ((h (intersect s pt xr yr zr)))
        (when h
          (let ((d (distance h pt)))
            (when (or (null dist) (< d dist))
              (setf surface s hit h dist d))))))
    (values surface hit)))

If you are starting to be skeptical about the much-discussed beauty of
Lisp, I don't blame you.  With Graham, you are learning a weird subset
of Common Lisp that isn't tremendously relevant to real CL
programming.

If you avoid Graham, you also get to miss out on the snide writing
style, the aversion to CLOS and objects in general, the inexplicable
hatred of COND, overzealous use of recursion, horrifically-named
variables and functions (like H and S and COMPR above), and the
signature twistly-twindly control flows.

If you're still not convinced, at least Peter Seibel doesn't have an
entire page dedicated to explaining why his style is so nutty:

http://www.cs.northwestern.edu/academics/courses/325/readings/graham/graham-notes.html

My (very un-humble) opinion is that you should learn from PCL.  Then,
when you know what to be on the lookout for, read On Lisp and ANSI
Common Lisp.  Read PAIP even before that.
From: jayessay
Subject: Re: Another n00b Q: lambda functions
Date: 
Message-ID: <m3vekzryom.fsf@rigel.goldenthreadtech.com>
"Bill Atkins" <·········@gmail.com> writes:

> For serious.  Why do you people even bother with Graham's books?

Well, _Ansi CL_ may not be much, but _On Lisp_ IMO is one of the 3 or
4 best Lisp books written.


/Jon

-- 
'j' - a n t h o n y at romeo/charley/november com
From: Pedro Kröger
Subject: Re: Another n00b Q: lambda functions
Date: 
Message-ID: <87lklxwoaw.fsf@gmail.com>
Miss Elaine Eos <····@*your-pants*PlayNaked.com> writes:

>> Error in process listener(1): Undefined function FOO called with
>> arguments (A) . While executing: FN-ALL

you don't need to use (function foo), i.e. #'foo inside fn-all:

    (apply foo (list obj))

or can use funcall:

    (funcall foo obj))))


Pedro Kroger
From: Frank Buss
Subject: Re: Another n00b Q: lambda functions
Date: 
Message-ID: <1a6ijlk7xy70x.1ti3khp5iax3v$.dlg@40tude.net>
Miss Elaine Eos wrote:

>> (setf abc (list 'a 'b 'c))

Note that SETF updates ABC, but it is undefined, if ABC is not a dynamic
variable (but many REPLs defines it, if not defined). You can use
defparameter or defvar for creating new dynamic variables (and defconstant
for global constants).

Some more examples for lambda: http://www.frank-buss.de/lisp/texture.html

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de