From: phal
Subject: what the following code do
Date: 
Message-ID: <1129525822.634620.196570@g14g2000cwa.googlegroups.com>
Hi, I have a lisp code which I don't understand clearly,
It seems to me that the lisp call the function and due to the function
it will display the message,
if the function g is call then it only display "G"
if the function h call it will display " G" and then follow by " H" ,
please explain me detail about this if you know it is wrong.
Thank again
What the definition does and the line below???
--------------------


(defun f()
(labels(
(g() (format t "G"))
(h() (g)(format t "H" )))
#'lambda(message)
(cond ((eq message 'g)
#'g)
((eq message ' h)
#'h)))))
(funcall (funcall(f)'h))
--------------
phal

From: Boris de Laar
Subject: Re: what the following code do
Date: 
Message-ID: <43539682$0$9450$ba620dc5@text.nova.planet.nl>
Hi phal,

phal wrote:
> Hi, I have a lisp code which I don't understand clearly,
> It seems to me that the lisp call the function and due to the function
> it will display the message,
> if the function g is call then it only display "G"
> if the function h call it will display " G" and then follow by " H" ,
> please explain me detail about this if you know it is wrong.
> Thank again
> What the definition does and the line below???

Things start to become clearer when you
1) use correct English, people will understand you better
2) use Emacs to indent and format the code nicely.

For example:

> (defun f()
> (labels(
> (g() (format t "G"))
> (h() (g)(format t "H" )))
> #'lambda(message)
> (cond ((eq message 'g)
> #'g)
> ((eq message ' h)
> #'h)))))
> (funcall (funcall(f)'h))

becomes

(defun f ()
  (labels ((g () (format t "G"))
	   (h () (g) (format t "H")))
    #'(lambda (message)
	(cond ((eq message 'g)
	       #'g)
	      ((eq message ' h)
	       #'h)))))

(funcall (funcall (f) 'h))

This should help you understand it.

Regards,
Boris de Laar
From: Pascal Bourguignon
Subject: Re: what the following code do
Date: 
Message-ID: <878xwsb89p.fsf@thalassa.informatimago.com>
Boris de Laar <·······@gmail.com> writes:

> Hi phal,
>
> phal wrote:
>> Hi, I have a lisp code which I don't understand clearly,
>> It seems to me that the lisp call the function and due to the function
>> it will display the message,
>> if the function g is call then it only display "G"
>> if the function h call it will display " G" and then follow by " H" ,
>> please explain me detail about this if you know it is wrong.
>> Thank again
>> What the definition does and the line below???
>
> Things start to become clearer when you
> 1) use correct English, people will understand you better
> 2) use Emacs to indent and format the code nicely.
>
> For example:
>
>> (defun f()
>> (labels(
>> (g() (format t "G"))
>> (h() (g)(format t "H" )))
>> #'lambda(message)
>> (cond ((eq message 'g)
>> #'g)
>> ((eq message ' h)
>> #'h)))))
>> (funcall (funcall(f)'h))
>
> becomes
>
> (defun f ()
>   (labels ((g () (format t "G"))
> 	   (h () (g) (format t "H")))
>     #'(lambda (message)
> 	(cond ((eq message 'g)
> 	       #'g)
> 	      ((eq message ' h)
> 	       #'h)))))
>
> (funcall (funcall (f) 'h))
>
> This should help you understand it.

You didn't let emacs indent it right, and you added a parenthesis.
Parenthesis are significant in lisp: you've changed the meaning!

The correct indentation is :

(defun f()
  (labels(
          (g() (format t "G"))
          (h() (g)(format t "H" )))
    #'lambda
    (message)
    (cond ((eq message 'g)
           #'g)
          ((eq message ' h)
           #'h))))

)
(funcall (funcall(f)'h))

With explanations:

(defun f()
  (labels(
          (g() (format t "G"))
          (h() (g)(format t "H" )))
    #'lambda      ; evaluates (function lambda), 
                  ; which returns (function lambda), 
                  ; which is ignored.
    (message)     ; call the function message with no argument.
                  ; if there's no such function, raises an error.
    (cond ((eq message 'g)  ; test the variable message
                            ; if there's no such variable bound 
                            ; then raise an error
           #'g)
          ((eq message ' h) ; idem
           #'h))))

)          ; stray closing parenthesis: raise an error.

(funcall (funcall(f)'h)) ; call the function f with no argument
                         ; and expect to get a function as result.
                         ; since f raises an error in all cases,
                         ; nothing more will happen.

         
                
-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
The mighty hunter
Returns with gifts of plump birds,
Your foot just squashed one.
From: Barry Margolin
Subject: Re: what the following code do
Date: 
Message-ID: <barmar-B1A4FF.15271717102005@comcast.dca.giganews.com>
In article <··············@thalassa.informatimago.com>,
 Pascal Bourguignon <····@mouse-potato.com> wrote:

> Boris de Laar <·······@gmail.com> writes:
> 
> > Hi phal,
> >
> > phal wrote:
> >> Hi, I have a lisp code which I don't understand clearly,
> >> It seems to me that the lisp call the function and due to the function
> >> it will display the message,
> >> if the function g is call then it only display "G"
> >> if the function h call it will display " G" and then follow by " H" ,
> >> please explain me detail about this if you know it is wrong.
> >> Thank again
> >> What the definition does and the line below???
> >
> > Things start to become clearer when you
> > 1) use correct English, people will understand you better
> > 2) use Emacs to indent and format the code nicely.
> >
> > For example:
> >
> >> (defun f()
> >> (labels(
> >> (g() (format t "G"))
> >> (h() (g)(format t "H" )))
> >> #'lambda(message)
> >> (cond ((eq message 'g)
> >> #'g)
> >> ((eq message ' h)
> >> #'h)))))
> >> (funcall (funcall(f)'h))
> >
> > becomes
> >
> > (defun f ()
> >   (labels ((g () (format t "G"))
> > 	   (h () (g) (format t "H")))
> >     #'(lambda (message)
> > 	(cond ((eq message 'g)
> > 	       #'g)
> > 	      ((eq message ' h)
> > 	       #'h)))))
> >
> > (funcall (funcall (f) 'h))
> >
> > This should help you understand it.
> 
> You didn't let emacs indent it right, and you added a parenthesis.
> Parenthesis are significant in lisp: you've changed the meaning!

I think he realized that the OP made a typo and left out that 
parenthesis when transcribing the function.  The code as originally 
posted is nonsense, he tried to answer the question of what the most 
likely actual code looked like (although he did mess up the indentation, 
probably because he used TABs instead of spaces).

Your explanation of what the mistyped function does is not very helpful.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***