From: Jeff Kish
Subject: learning question new guy...
Date: 
Message-ID: <Y3nFPwweCrkZOefMPM4kJckQmN60@4ax.com>
I found the below exercises on the 'net.
I can't figure out how to do 'b'.
Can someone give me some pointers?

"
Using first and rest extract the atom ``jim'' from the following:
a.
(he is dead jim)
b.
(captain (((jim) kirk)))
"


I tried a number of things, but if I leave the basic
phrase alone, it just does not quite divvy up (I 
think I can't get the items separated with just 'first' and 'rest'.
Thanks

From: Kenny Tilton
Subject: Re: learning question new guy...
Date: 
Message-ID: <O9exb.155085$Gq.19567404@twister.nyc.rr.com>
Jeff Kish wrote:
> I found the below exercises on the 'net.
> I can't figure out how to do 'b'.
> Can someone give me some pointers?
> 
> "
> Using first and rest extract the atom ``jim'' from the following:
> a.
> (he is dead jim)
> b.
> (captain (((jim) kirk)))
> "
> 
> 
> I tried a number of things, but if I leave the basic
> phrase alone, it just does not quite divvy up (I 
> think I can't get the items separated with just 'first' and 'rest'.

Well, you can, but you have to use them more than once, digging deeper 
into the nested lists as you go. for example:

(first (first (first '((((a))))))) => (A)

ie, close but no cigar. I need another (first ...) around the whole thing.

in your case you need a combo of firsts/rests. btw, check out the 
functions such as caaar, which does (car (car (car...))) aka (first 
(first (first...))). you can mix them up with cdrs (rests) as in cadar 
as well.

kt

-- 
http://tilton-technology.com

Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film

Your Project Here! http://alu.cliki.net/Industry%20Application
From: Kaz Kylheku
Subject: Re: learning question new guy...
Date: 
Message-ID: <cf333042.0311271044.348e9e1e@posting.google.com>
Jeff Kish <·········@earthlink.net> wrote in message news:<····························@4ax.com>...
> I found the below exercises on the 'net.
> I can't figure out how to do 'b'.

I can't either; I get bored just thinking about it. Such artificial
exercises are for computing children. In Lisp, we find the easiest,
most expressive way to do something. The language has a heck of a lot
more tools in it than FIRST and REST.

> Can someone give me some pointers?
> 
> "
> Using first and rest extract the atom ``jim'' from the following:
> a.
> (he is dead jim)
> b.
> (captain (((jim) kirk)))
> "

For pulling out things from a fixed, nested list structure, there is
an operator called DESTRUCTURING-BIND. This operator lets you specify
a pattern list which matches the structure of the list that you want
to analyze. In the pattern, you put variable symbols. The operator
extracts the corresponding parts of the analyzed list, and binds them
to the variables named in the pattern. Then zero or more body forms
are evaluated in a scope where these variables are visible.

  (destructuring-bind (rank (((first-name) last-name)))
                      '(captain (((jim) kirk)))
    (format t "rank: ~a, first-name: ~a, last-name: ~a~%"
            rank first-name last-name))

This oeprator is a macro which spits out list-cracking code---code
that you would have to write by hand otherwise! You can use
MACROEXPAND to investigate what code is generated and substituted for
the form:

  (macroexpand
    '(destructuring-bind (rank (((first-name) last-name)))
                         '(captain (((jim) kirk)))
       (format t "rank: ~a, first-name: ~a, last-name: ~a~%"
               rank first-name last-name)))

Buried in the result you will find some expression that fetches the
captain's first name and binds it to the FIRST-NAME variable. Chances
are it will use the ``cadaver'' functions: CAAAADR and such. They are
called cadavers because they resemble the word and because they are
gruesome. :)
From: Jeff Kish
Subject: Re: learning question new guy...
Date: 
Message-ID: <qVPGP==V8FlNdqD2tLotjLv8uJe0@4ax.com>
On 27 Nov 2003 10:44:48 -0800, ···@ashi.footprints.net (Kaz Kylheku) wrote:

>Jeff Kish <·········@earthlink.net> wrote in message news:<····························@4ax.com>...
>> I found the below exercises on the 'net.
>> I can't figure out how to do 'b'.
>
>I can't either; I get bored just thinking about it. Such artificial
>exercises are for computing children. In Lisp, we find the easiest,
>most expressive way to do something. The language has a heck of a lot
>more tools in it than FIRST and REST.
>
>> Can someone give me some pointers?
>> 
>> "
>> Using first and rest extract the atom ``jim'' from the following:
>> a.
>> (he is dead jim)
>> b.
>> (captain (((jim) kirk)))
>> "
>
>For pulling out things from a fixed, nested list structure, there is
>an operator called DESTRUCTURING-BIND. This operator lets you specify
>a pattern list which matches the structure of the list that you want
>to analyze. In the pattern, you put variable symbols. The operator
>extracts the corresponding parts of the analyzed list, and binds them
>to the variables named in the pattern. Then zero or more body forms
>are evaluated in a scope where these variables are visible.
>
>  (destructuring-bind (rank (((first-name) last-name)))
>                      '(captain (((jim) kirk)))
>    (format t "rank: ~a, first-name: ~a, last-name: ~a~%"
>            rank first-name last-name))
>
>This oeprator is a macro which spits out list-cracking code---code
>that you would have to write by hand otherwise! You can use
>MACROEXPAND to investigate what code is generated and substituted for
>the form:
>
>  (macroexpand
>    '(destructuring-bind (rank (((first-name) last-name)))
>                         '(captain (((jim) kirk)))
>       (format t "rank: ~a, first-name: ~a, last-name: ~a~%"
>               rank first-name last-name)))
>
>Buried in the result you will find some expression that fetches the
>captain's first name and binds it to the FIRST-NAME variable. Chances
>are it will use the ``cadaver'' functions: CAAAADR and such. They are
>called cadavers because they resemble the word and because they are
>gruesome. :)
Thanks. I am doing the exercises because they are there, and I need 
a good way to learn lisp. Also I find sometimes repetition sometimes hammers 
stuff home and I'd like to have a good grip on the basics.

Lisp looks pretty powerful!
From: Wolfhard Buß
Subject: Re: learning question new guy...
Date: 
Message-ID: <m3brqyrsz8.fsf@buss-14250.user.cis.dfn.de>
* Jeff Kish:
> I found the below exercises on the 'net.
:
> Using first and rest extract the atom ``jim'' from the following:
> (captain (((jim) kirk)))

Lispniks know, that jim is the car of the car of the car of the car of
the cdr of (captain (((jim) kirk))). Unfortunately, the otherwise fine
Standard doesn't mention caaaadr - it doesn't even mention nthcar
(Lispniks know, that jim is the 4th nthcar of the cdr of (captain
(((jim) kirk)))).  What next? defun?


-- 
"Hurry if you still want to see something. Everything is vanishing."
                                       --  Paul C�zanne (1839-1906)
From: Jeff Kish
Subject: Re: learning question new guy...
Date: 
Message-ID: <VlPGPxxQF0Jn9TLIpmnp4fhqM0Xr@4ax.com>
On Wed, 26 Nov 2003 22:11:55 -0600, Jeff Kish <·········@earthlink.net> wrote:

>I found the below exercises on the 'net.
>I can't figure out how to do 'b'.
>Can someone give me some pointers?
>
>"
>Using first and rest extract the atom ``jim'' from the following:
>a.
>(he is dead jim)
>b.
>(captain (((jim) kirk)))
>"
>
>
>I tried a number of things, but if I leave the basic
>phrase alone, it just does not quite divvy up (I 
>think I can't get the items separated with just 'first' and 'rest'.
>Thanks
Thanks for your replies. I did not realize that 
the functions first etc. were stripping off 
sets of outer parenthesis each time they 
were used.
Jeff
From: Kaz Kylheku
Subject: Re: learning question new guy...
Date: 
Message-ID: <cf333042.0311271638.6192b126@posting.google.com>
Jeff Kish <·········@earthlink.net> wrote in message news:<····························@4ax.com>...
> Thanks for your replies. I did not realize that 
> the functions first etc. were stripping off 
> sets of outer parenthesis each time they 
> were used.

Parentheses are just the printed notation. The list objects themselves
don't contain any parentheses! You are manipulating data structures,
not character strings.

The list (captain (((jim) kirk))) corresponds to the structure

    [  |  ]
    /     \   
  CAPTAIN  [  | NIL ]
            /
       [  | NIL ]
       /   
    [  |  ]
     /    \
[  | NIL ]  [  | NIL ]
  /          /  
JIM        KIRK  

The [ | ] things stand for cons cells: FIRST gives you the left value,
REST the right one. Start at the root of the tree. To get to JIM, you
have to go right, left, left, left, left. In other words: rest, first,
first, first, first. Or (first (first (first (first (rest '(captain
(((jim) kirk)))))))).

Probably the most useful thing you can do for yourself is to learn to
draw these tree diagrams from symbolic expressions, to help you
visualize the objects in memory and how they point to each other.
From: Alan Crowe
Subject: Re: learning question new guy...
Date: 
Message-ID: <86he0pwsrd.fsf@cawtech.freeserve.co.uk>
Jeff Kish asked:

    Using first and rest extract the atom ``jim'' from the following:
    a.
    (he is dead jim)
    b.
    (captain (((jim) kirk)))

The read-eval-print-loop gives you variables *, **, and ***
for the previous three results, so you can do:

* '(a b c)
(A B C)
* (first *)
A
* (rest **)
(B C)

With this technique you can use brute force on the problem

* '(captain (((jim) kirk)))
(CAPTAIN (((JIM) KIRK)))
* (first *)
CAPTAIN
* (rest **)
((((JIM) KIRK)))

obviously we start with rest

* (rest ***)
((((JIM) KIRK)))
* (first *)
(((JIM) KIRK))
* (rest **)
NIL

then we need a first, and so on.

Alan Crowe
From: Tim Bradshaw
Subject: Re: learning question new guy...
Date: 
Message-ID: <ey3n0ahe8jr.fsf@lostwithiel.cley.com>
* Jeff Kish wrote:
> Can someone give me some pointers?

Use the tfb magic cons tree destructurator (originally designed for
answering homework questions...):

(defun cons-tree-destructurator (item form vname &key 
                                      (test #'eql) 
                                      (order ':depth-first))
  (ecase order
    ((:depth-first)
     (block done
       (labels ((mf (f q)
                  (cond ((funcall test f item)
                         (return-from done q))
                       ((consp f)
                        (mf (car f) (list 'first q))
                        (mf (cdr f) (list 'rest q)))
                       (t nil))))
         (mf form vname)
         nil)))
    ((:breadth-first)
     ;; this is breadth-first in terms of *conses*, not lists
     (block done
       (labels ((mf (a)
                  (mf
                   (loop for (f . q) in a
                         when (funcall test f item)
                         do (return-from done q)
                         when (consp f)
                         collect (cons (car f)
                                       (list 'first q))
                         collect (cons (cdr f)
                                       (list 'rest q))))))
         (mf `((,form . ,vname))))))))