From: Lyman S. Taylor
Subject: Re: Need help
Date: 
Message-ID: <78lreh$ek8@pravda.cc.gatech.edu>
In article <·················@mail.usask.ca>,
Hsing-Chan Huang  <······@mail.usask.ca> wrote:
>Hi all :
>    I am a lisp newbies, and recently i have problem with a function i
>wrote.
>because i dont know where goes wrong .....please help me..

  The following smells of a homework problem.  I'll refrain from
  the direct answer.

>
>    The purpose of my lisp function is to compose  any 2 lisp function
>into 1 function

     Is "any"  a tad tooo broad?  I presume you mean compose any two 
     one argument functions into a third function of one argument. 

      (defun f2 ( x y ) ... )  
      (defun g3 ( x y x ) ... ) 

     aren't going to compose too well. 


>The lambda expression i got from the  compose function i wrote is
>>(compose 'g 'f)
>(LAMBDA-BLOCK G (LAMBDA-BLOCK F (X) (CDR X)) (CAR X))
>and i dont know if it is in the right format ? help me plez

   You can ask lisp to answer this question for you.  There is a 
   predicate function called  FUNCTIONP.   It will return T if and 
   only if the argument is an object that is a function. [ Actually, in
   other cases too, but that's not important. ] 

   (functionp #'car )  ==> T
 
   (functionp #'(lambda (x) (+ x x )) )   ==>  T 
   
   (functionp '(lambda (x) (+ x x )))  ==> NIL 

  Secondly, 

       Does the following: 

         (defun  try-me ( x )  
              (cdr x )
              (car x ) )

       return the correct answer when passed your test list?

       (try-me '( A B C D ))  ==>  ?     

       What does the following produce:

        (defun try-me-too ( x ) 
             (cdr x )
              x  )

       (try-me-too '(A B C D E) ) ==> ? 


   Thirdly, you should be familiar with the functions FUNCALL and APPLY. 

   For instance 

   (funcall #'(lambda (x) (+ x x ))  2 )  ==> 4 


P.S.  Implementation that are up the ANSI CL standard will take the following

                (functionp (lambda (x) (+ x x )) )   ==>  T 

P.P.S.   #'FOO   is lisp shorthad for   (FUNCTION FOO) 

-- 
					
Lyman S. Taylor          "The Borg --  party poopers of the Galaxy. "
(·····@cc.gatech.edu)                 EMH Doctor  Star Trek Voyager. 

From: Chris
Subject: Re: Need help
Date: 
Message-ID: <36AE7A6A.809B0518@mail.usask.ca>
Hi :
    I am really appreciated your responds and indeed it is a assignment Q
.......*^_^*
I really have no idea onn how to compose two 1 arguement functions into a 1
arguement function.
and how they pass the varaibles within the function.
    Is the  " #'lambda " equals to the "( lambda-block .........) if we type
(symbol-function 'x) ?

also i have been looking everywhere and try to find more infos about the "lambda"
stuff , do you know anywhere i can find it ?


thanx
Chris

"Lyman S. Taylor" wrote:

> In article <·················@mail.usask.ca>,
> Hsing-Chan Huang  <······@mail.usask.ca> wrote:
> >Hi all :
> >    I am a lisp newbies, and recently i have problem with a function i
> >wrote.
> >because i dont know where goes wrong .....please help me..
>
>   The following smells of a homework problem.  I'll refrain from
>   the direct answer.
>
> >
> >    The purpose of my lisp function is to compose  any 2 lisp function
> >into 1 function
>
>      Is "any"  a tad tooo broad?  I presume you mean compose any two
>      one argument functions into a third function of one argument.
>
>       (defun f2 ( x y ) ... )
>       (defun g3 ( x y x ) ... )
>
>      aren't going to compose too well.
>
> >The lambda expression i got from the  compose function i wrote is
> >>(compose 'g 'f)
> >(LAMBDA-BLOCK G (LAMBDA-BLOCK F (X) (CDR X)) (CAR X))
> >and i dont know if it is in the right format ? help me plez
>
>    You can ask lisp to answer this question for you.  There is a
>    predicate function called  FUNCTIONP.   It will return T if and
>    only if the argument is an object that is a function. [ Actually, in
>    other cases too, but that's not important. ]
>
>    (functionp #'car )  ==> T
>
>    (functionp #'(lambda (x) (+ x x )) )   ==>  T
>
>    (functionp '(lambda (x) (+ x x )))  ==> NIL
>
>   Secondly,
>
>        Does the following:
>
>          (defun  try-me ( x )
>               (cdr x )
>               (car x ) )
>
>        return the correct answer when passed your test list?
>
>        (try-me '( A B C D ))  ==>  ?
>
>        What does the following produce:
>
>         (defun try-me-too ( x )
>              (cdr x )
>               x  )
>
>        (try-me-too '(A B C D E) ) ==> ?
>
>    Thirdly, you should be familiar with the functions FUNCALL and APPLY.
>
>    For instance
>
>    (funcall #'(lambda (x) (+ x x ))  2 )  ==> 4
>
> P.S.  Implementation that are up the ANSI CL standard will take the following
>
>                 (functionp (lambda (x) (+ x x )) )   ==>  T
>
> P.P.S.   #'FOO   is lisp shorthad for   (FUNCTION FOO)
>
> --
>
> Lyman S. Taylor          "The Borg --  party poopers of the Galaxy. "
> (·····@cc.gatech.edu)                 EMH Doctor  Star Trek Voyager.
From: Kent M Pitman
Subject: Re: Need help
Date: 
Message-ID: <sfw4spcg8c4.fsf@world.std.com>
Chris <······@mail.usask.ca> writes:

>     I am really appreciated your responds and indeed it is a assignment Q

It is bad etiquette on this group or any other public forum to
request help with a homework problem and not say so.  If you 
do not say it is homework, people might think you are cheating
by trying to get someone else to do your homework.  If someone
does respond and later finds out it is homework, they may feel
they are party to helping you "cheat".  Next time you or anyone
has a homework problem they need help with, you should say so
before someone has to ask you.
From: Chris
Subject: Re: Need help
Date: 
Message-ID: <36AF3DFA.D71CED94@mail.usask.ca>
Hmm......I know its wrong to ask about homework.and i am totally agreed with
you
But i did my homeworks, althought it was totally wrong because i didn't  have
the right idea
    Therefore i posted the message here to ask for "directions" on how to code
such a question..

And that what Lyman S. Taylor did , and i really learn a lot............thanx
guys

Chris

Kent M Pitman wrote:

> Chris <······@mail.usask.ca> writes:
>
> >     I am really appreciated your responds and indeed it is a assignment Q
>
> It is bad etiquette on this group or any other public forum to
> request help with a homework problem and not say so.  If you
> do not say it is homework, people might think you are cheating
> by trying to get someone else to do your homework.  If someone
> does respond and later finds out it is homework, they may feel
> they are party to helping you "cheat".  Next time you or anyone
> has a homework problem they need help with, you should say so
> before someone has to ask you.
From: Chris
Subject: Re: Need help
Date: 
Message-ID: <36AE7C69.EA0F8DB6@mail.usask.ca>
Sorry to bother you again....

for example  CAR
how do we know the lambda expression of  CAR  ?
its a complied function and return nothing when i type " (symbol-function 'car)
"....

because what we need to do is to compose (eg  car & cdr ) into a single
function....

thanx
Chris

"Lyman S. Taylor" wrote:

> In article <·················@mail.usask.ca>,
> Hsing-Chan Huang  <······@mail.usask.ca> wrote:
> >Hi all :
> >    I am a lisp newbies, and recently i have problem with a function i
> >wrote.
> >because i dont know where goes wrong .....please help me..
>
>   The following smells of a homework problem.  I'll refrain from
>   the direct answer.
>
> >
> >    The purpose of my lisp function is to compose  any 2 lisp function
> >into 1 function
>
>      Is "any"  a tad tooo broad?  I presume you mean compose any two
>      one argument functions into a third function of one argument.
>
>       (defun f2 ( x y ) ... )
>       (defun g3 ( x y x ) ... )
>
>      aren't going to compose too well.
>
> >The lambda expression i got from the  compose function i wrote is
> >>(compose 'g 'f)
> >(LAMBDA-BLOCK G (LAMBDA-BLOCK F (X) (CDR X)) (CAR X))
> >and i dont know if it is in the right format ? help me plez
>
>    You can ask lisp to answer this question for you.  There is a
>    predicate function called  FUNCTIONP.   It will return T if and
>    only if the argument is an object that is a function. [ Actually, in
>    other cases too, but that's not important. ]
>
>    (functionp #'car )  ==> T
>
>    (functionp #'(lambda (x) (+ x x )) )   ==>  T
>
>    (functionp '(lambda (x) (+ x x )))  ==> NIL
>
>   Secondly,
>
>        Does the following:
>
>          (defun  try-me ( x )
>               (cdr x )
>               (car x ) )
>
>        return the correct answer when passed your test list?
>
>        (try-me '( A B C D ))  ==>  ?
>
>        What does the following produce:
>
>         (defun try-me-too ( x )
>              (cdr x )
>               x  )
>
>        (try-me-too '(A B C D E) ) ==> ?
>
>    Thirdly, you should be familiar with the functions FUNCALL and APPLY.
>
>    For instance
>
>    (funcall #'(lambda (x) (+ x x ))  2 )  ==> 4
>
> P.S.  Implementation that are up the ANSI CL standard will take the following
>
>                 (functionp (lambda (x) (+ x x )) )   ==>  T
>
> P.P.S.   #'FOO   is lisp shorthad for   (FUNCTION FOO)
>
> --
>
> Lyman S. Taylor          "The Borg --  party poopers of the Galaxy. "
> (·····@cc.gatech.edu)                 EMH Doctor  Star Trek Voyager.
From: Lyman S. Taylor
Subject: Re: Need help
Date: 
Message-ID: <78m2lc$ets@pravda.cc.gatech.edu>
In article <·················@mail.usask.ca>,
Chris  <······@mail.usask.ca> wrote:
>for example  CAR
>how do we know the lambda expression of  CAR  ?
>its a complied function and return nothing when i type " (symbol-function 'car)
>"....
>
>because what we need to do is to compose (eg  car & cdr ) into a single
>function....

 Short Answer: 

    You don't "need" the body of the functon to do a composition.
    You need to create a function.  Not construct a body. The two are
    necessarily the same. 

 Short Reference:

     The following is a lecture on "lambda" from a Ga. Tech class.

     http://www.cc.gatech.edu/computing/classes/cs2360/fall98/lec07.html

     [ The following is my own rehash of the above with some particulars
         to the problem at hand. ] 

 Longer Answer:

   Do you want to compose two functions to create a new function to 
   "look at" (or treat as data).  Or do you want to compose two functions
   to "use"  ( pass data to and produce answers)?

   I presumed it was the latter you wanted. 


    [ NOTE: the "?" below is the lisp listener prompt  ] 

    ? (defun foo (x) (+ x x ))
    FOO
    ? (function foo )   ;; roughly equiv to  (symbol-function 'foo)
       ... some implementation representation of a functon.... 

 What the Listener prints depends upon whether your implementatin compiles
 this definition or constructs something to be interpreted later. 
 In Allegro CL the "answer" printed is 

    USER(2): (function foo)
    #<Interpreted Function FOO>

 In Lispworks 
    
    > (function foo)
    #<Interpreted-Function FOO 9A0A56>

  In MCL (which I don't have handy but would print something like). 

     ? (function foo)
     #<Compiled-Function FOO ...> 

 No matter how printed differently by the various implementations they
 all are functions.  When passed to FUNCTIONP the result will be 
 T.   

 From your output I get the impressoin that your implementation prints 
 more of the body of the interpreted version of your code by default. 

 If you original examples for  F and   G are compiled 

    ? (compile 'f) 

    ? (compile 'g) 

 I think that your composition function is still suppose to  
 work (you want a function to use, not "look at") and will return a 
 answer that says "compiled". 

 The function LIST  will produce a list as a result.

     ?  (list 'a 'b 'c) 
      ( A B C )

 You can use LAMBDA to create a function. ( A function is just a different
 type of data. It really not to much differentin this respect from 
  list, integer, float, etc.). 

      ? #'(lambda (x ) (+ x x))
       #<Interpreted Function (unamed) ... > 

  LAMBDA creates a function with "no name", but a function none-the-less.
  [ some implementation may print this so that it resembles a list.
     It is not a list.  If passed to LISTP the answer will be NIL. ]
  You can attach this function to a name using an expression found in
  your original message. 

      ? (setf  (symbol-function 'h ) #'(lambda (x ) (+ x x )) )
      #<Interpreted Function H > 

    [ By the way you didn't have to defined H beforehand. I think the
      above should work. ]

      ? (h 2 )
       4 
    
     [ the above could have been done in one step as... ]
     
      ? (funcall #'(lambda (x) (+ x x))  2 )

       4 

     In this latter case attaching the result to "H" wasn't really
     an interesting step.  A "no named" function could do the job
     quite nicely. 


  


  
  
  


  





    
>
>thanx
>Chris
>
>"Lyman S. Taylor" wrote:
>
>> In article <·················@mail.usask.ca>,
>> Hsing-Chan Huang  <······@mail.usask.ca> wrote:
>> >Hi all :
>> >    I am a lisp newbies, and recently i have problem with a function i
>> >wrote.
>> >because i dont know where goes wrong .....please help me..
>>
>>   The following smells of a homework problem.  I'll refrain from
>>   the direct answer.
>>
>> >
>> >    The purpose of my lisp function is to compose  any 2 lisp function
>> >into 1 function
>>
>>      Is "any"  a tad tooo broad?  I presume you mean compose any two
>>      one argument functions into a third function of one argument.
>>
>>       (defun f2 ( x y ) ... )
>>       (defun g3 ( x y x ) ... )
>>
>>      aren't going to compose too well.
>>
>> >The lambda expression i got from the  compose function i wrote is
>> >>(compose 'g 'f)
>> >(LAMBDA-BLOCK G (LAMBDA-BLOCK F (X) (CDR X)) (CAR X))
>> >and i dont know if it is in the right format ? help me plez
>>
>>    You can ask lisp to answer this question for you.  There is a
>>    predicate function called  FUNCTIONP.   It will return T if and
>>    only if the argument is an object that is a function. [ Actually, in
>>    other cases too, but that's not important. ]
>>
>>    (functionp #'car )  ==> T
>>
>>    (functionp #'(lambda (x) (+ x x )) )   ==>  T
>>
>>    (functionp '(lambda (x) (+ x x )))  ==> NIL
>>
>>   Secondly,
>>
>>        Does the following:
>>
>>          (defun  try-me ( x )
>>               (cdr x )
>>               (car x ) )
>>
>>        return the correct answer when passed your test list?
>>
>>        (try-me '( A B C D ))  ==>  ?
>>
>>        What does the following produce:
>>
>>         (defun try-me-too ( x )
>>              (cdr x )
>>               x  )
>>
>>        (try-me-too '(A B C D E) ) ==> ?
>>
>>    Thirdly, you should be familiar with the functions FUNCALL and APPLY.
>>
>>    For instance
>>
>>    (funcall #'(lambda (x) (+ x x ))  2 )  ==> 4
>>
>> P.S.  Implementation that are up the ANSI CL standard will take the following
>>
>>                 (functionp (lambda (x) (+ x x )) )   ==>  T
>>
>> P.P.S.   #'FOO   is lisp shorthad for   (FUNCTION FOO)
>>
>> --
>>
>> Lyman S. Taylor          "The Borg --  party poopers of the Galaxy. "
>> (·····@cc.gatech.edu)                 EMH Doctor  Star Trek Voyager.
>


-- 
					
Lyman S. Taylor          "The Borg --  party poopers of the Galaxy. "
(·····@cc.gatech.edu)                 EMH Doctor  Star Trek Voyager. 
From: Chris
Subject: Re: Need help
Date: 
Message-ID: <36AEA03F.F8F9B39D@mail.usask.ca>
Sorry to bother you again.....

thanx for all the useful explainations, its very helpful

this is the original question :
Write a LISP function (COMPOSE F G) that returns the function composition of F and
G, i.e.,
> (defun f (X) (cdr X))
F

> (defun g (X) (car X))
G

> (defun h nil nil) ; we must define h before we can do a symbol-function

> (setf (symbol-function 'h) (compose 'g 'f))

This should return a lambda expression that represents the composition.

> (h '(this is a list))
IS

Furthermore, this function composition should be robust, i.e., it should not change
even if you change the definitions of F and G. (Just as a variable should not change
when a variable originally assigned to it is changed):
>(defun f(X) (cdddr X))
F

>(h '(this is a list))
IS

+++++++++++++++++++++++++++=
I am totally lost (in this Q).........because the "compose" takes 2 arguements but
the "h " take 1...
and  i was unable to code a "compose function " which will return the composed
lambda express of f & g

thanx
Chris



















"Lyman S. Taylor" wrote:

> In article <·················@mail.usask.ca>,
> Chris  <······@mail.usask.ca> wrote:
> >for example  CAR
> >how do we know the lambda expression of  CAR  ?
> >its a complied function and return nothing when i type " (symbol-function 'car)
> >"....
> >
> >because what we need to do is to compose (eg  car & cdr ) into a single
> >function....
>
>  Short Answer:
>
>     You don't "need" the body of the functon to do a composition.
>     You need to create a function.  Not construct a body. The two are
>     necessarily the same.
>
>  Short Reference:
>
>      The following is a lecture on "lambda" from a Ga. Tech class.
>
>      http://www.cc.gatech.edu/computing/classes/cs2360/fall98/lec07.html
>
>      [ The following is my own rehash of the above with some particulars
>          to the problem at hand. ]
>
>  Longer Answer:
>
>    Do you want to compose two functions to create a new function to
>    "look at" (or treat as data).  Or do you want to compose two functions
>    to "use"  ( pass data to and produce answers)?
>
>    I presumed it was the latter you wanted.
>
>     [ NOTE: the "?" below is the lisp listener prompt  ]
>
>     ? (defun foo (x) (+ x x ))
>     FOO
>     ? (function foo )   ;; roughly equiv to  (symbol-function 'foo)
>        ... some implementation representation of a functon....
>
>  What the Listener prints depends upon whether your implementatin compiles
>  this definition or constructs something to be interpreted later.
>  In Allegro CL the "answer" printed is
>
>     USER(2): (function foo)
>     #<Interpreted Function FOO>
>
>  In Lispworks
>
>     > (function foo)
>     #<Interpreted-Function FOO 9A0A56>
>
>   In MCL (which I don't have handy but would print something like).
>
>      ? (function foo)
>      #<Compiled-Function FOO ...>
>
>  No matter how printed differently by the various implementations they
>  all are functions.  When passed to FUNCTIONP the result will be
>  T.
>
>  From your output I get the impressoin that your implementation prints
>  more of the body of the interpreted version of your code by default.
>
>  If you original examples for  F and   G are compiled
>
>     ? (compile 'f)
>
>     ? (compile 'g)
>
>  I think that your composition function is still suppose to
>  work (you want a function to use, not "look at") and will return a
>  answer that says "compiled".
>
>  The function LIST  will produce a list as a result.
>
>      ?  (list 'a 'b 'c)
>       ( A B C )
>
>  You can use LAMBDA to create a function. ( A function is just a different
>  type of data. It really not to much differentin this respect from
>   list, integer, float, etc.).
>
>       ? #'(lambda (x ) (+ x x))
>        #<Interpreted Function (unamed) ... >
>
>   LAMBDA creates a function with "no name", but a function none-the-less.
>   [ some implementation may print this so that it resembles a list.
>      It is not a list.  If passed to LISTP the answer will be NIL. ]
>   You can attach this function to a name using an expression found in
>   your original message.
>
>       ? (setf  (symbol-function 'h ) #'(lambda (x ) (+ x x )) )
>       #<Interpreted Function H >
>
>     [ By the way you didn't have to defined H beforehand. I think the
>       above should work. ]
>
>       ? (h 2 )
>        4
>
>      [ the above could have been done in one step as... ]
>
>       ? (funcall #'(lambda (x) (+ x x))  2 )
>
>        4
>
>      In this latter case attaching the result to "H" wasn't really
>      an interesting step.  A "no named" function could do the job
>      quite nicely.
>
>
>
>
>
>
>
>
>
>
> >
> >thanx
> >Chris
> >
> >"Lyman S. Taylor" wrote:
> >
> >> In article <·················@mail.usask.ca>,
> >> Hsing-Chan Huang  <······@mail.usask.ca> wrote:
> >> >Hi all :
> >> >    I am a lisp newbies, and recently i have problem with a function i
> >> >wrote.
> >> >because i dont know where goes wrong .....please help me..
> >>
> >>   The following smells of a homework problem.  I'll refrain from
> >>   the direct answer.
> >>
> >> >
> >> >    The purpose of my lisp function is to compose  any 2 lisp function
> >> >into 1 function
> >>
> >>      Is "any"  a tad tooo broad?  I presume you mean compose any two
> >>      one argument functions into a third function of one argument.
> >>
> >>       (defun f2 ( x y ) ... )
> >>       (defun g3 ( x y x ) ... )
> >>
> >>      aren't going to compose too well.
> >>
> >> >The lambda expression i got from the  compose function i wrote is
> >> >>(compose 'g 'f)
> >> >(LAMBDA-BLOCK G (LAMBDA-BLOCK F (X) (CDR X)) (CAR X))
> >> >and i dont know if it is in the right format ? help me plez
> >>
> >>    You can ask lisp to answer this question for you.  There is a
> >>    predicate function called  FUNCTIONP.   It will return T if and
> >>    only if the argument is an object that is a function. [ Actually, in
> >>    other cases too, but that's not important. ]
> >>
> >>    (functionp #'car )  ==> T
> >>
> >>    (functionp #'(lambda (x) (+ x x )) )   ==>  T
> >>
> >>    (functionp '(lambda (x) (+ x x )))  ==> NIL
> >>
> >>   Secondly,
> >>
> >>        Does the following:
> >>
> >>          (defun  try-me ( x )
> >>               (cdr x )
> >>               (car x ) )
> >>
> >>        return the correct answer when passed your test list?
> >>
> >>        (try-me '( A B C D ))  ==>  ?
> >>
> >>        What does the following produce:
> >>
> >>         (defun try-me-too ( x )
> >>              (cdr x )
> >>               x  )
> >>
> >>        (try-me-too '(A B C D E) ) ==> ?
> >>
> >>    Thirdly, you should be familiar with the functions FUNCALL and APPLY.
> >>
> >>    For instance
> >>
> >>    (funcall #'(lambda (x) (+ x x ))  2 )  ==> 4
> >>
> >> P.S.  Implementation that are up the ANSI CL standard will take the following
> >>
> >>                 (functionp (lambda (x) (+ x x )) )   ==>  T
> >>
> >> P.P.S.   #'FOO   is lisp shorthad for   (FUNCTION FOO)
> >>
> >> --
> >>
> >> Lyman S. Taylor          "The Borg --  party poopers of the Galaxy. "
> >> (·····@cc.gatech.edu)                 EMH Doctor  Star Trek Voyager.
> >
>
> --
>
> Lyman S. Taylor          "The Borg --  party poopers of the Galaxy. "
> (·····@cc.gatech.edu)                 EMH Doctor  Star Trek Voyager.