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 purpose of my lisp function is to compose any 2 lisp function
into 1 function
eg :
> (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
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
so i was assuming that is right, then i type
> (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))
Error: (X) is not a symbol.
Fast links are on: do (si::use-fast-links nil) for debugging
Error signalled by G.
Broken at G. Type :H for Help.
I have try to debug it for 2 days but i still couldnt get it .....help
me please
thanx
Chris
In article <·················@mail.usask.ca>, ······@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 purpose of my lisp function is to compose any 2 lisp function
Which Lisp are you using and what is your definition of COMPOSE?
--
http://www.lavielle.com/~joswig
Hi :
thanx for answering my Q
the fellowing is my compose function but i dont know if it is right or
not..and i am using GCL
(defun compose (g f)
(cond
(t (find_x (get_symbol g) f))
))
(defun find_x (h k)
(cond
((listp (car h))(cons(get_symbol k)(cdr h) ) )
(t ( cons (car h) (find_x (cdr h) k)))
))
(defun get_symbol (m)
(cond
(t(symbol-function m ))
))
Rainer Joswig wrote:
> In article <·················@mail.usask.ca>, ······@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 purpose of my lisp function is to compose any 2 lisp function
>
> Which Lisp are you using and what is your definition of COMPOSE?
>
> --
> http://www.lavielle.com/~joswig
In article <·················@mail.usask.ca>, ······@mail.usask.ca wrote:
> Hi :
> thanx for answering my Q
>
> the fellowing is my compose function but i dont know if it is right or
> not..and i am using GCL
How about something like:
(defun compose (f g)
#'(lambda (&rest args)
(declare (dynamic-extent args))
(funcall f (apply g args))))
Example:
(funcall (compose #'first #'second)
'((1 2) (3 4)))
-> 3
--
http://www.lavielle.com/~joswig
i think i am lost now.....
this is the Q :
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
hope fully you can give me some advise on how to code the function compose
.......
thanx
Chris
Rainer Joswig wrote:
> In article <·················@mail.usask.ca>, ······@mail.usask.ca wrote:
>
> > Hi :
> > thanx for answering my Q
> >
> > the fellowing is my compose function but i dont know if it is right or
> > not..and i am using GCL
>
> How about something like:
>
> (defun compose (f g)
> #'(lambda (&rest args)
> (declare (dynamic-extent args))
> (funcall f (apply g args))))
>
> Example:
>
> (funcall (compose #'first #'second)
> '((1 2) (3 4)))
>
> -> 3
>
> --
> http://www.lavielle.com/~joswig
In article <·················@mail.usask.ca>,
Chris <······@mail.usask.ca> wrote:
>i think i am lost now.....
>
>this is the Q :
>
>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
You don't need to define h before you can *assign* to 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):
If you use #'g and #'f, rather than 'g and 'f, the COMPOSE function that
Rainer suggested will do this. #' extracts the functional binding, which
doesn't change even if you redefine the function name.
Why are you insisting on writing a function that caters to such poor style?
Furthermore, a function that accepts symbols won't do the right thing with
local functions.
But if you insist, the following should work:
(defun compose (f1 f2)
(flet ((get-function (func-or-name)
(if (typep 'function func-or-name)
func-or-name
(fdefinition func-or-name))))
(let ((func1 (get-function f1))
(func2 (get-function f2)))
#'(lambda (&rest args)
(declare (dynamic-extent args))
(funcall func1 (apply func2 args))))))
This allows you to provide either function objects or function names
(either symbols or lists of the form (SETF symbol)). If a name is
supplied, the definition is extracted when COMPOSE is called, and saved
away in the closure.
--
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.
Sorry to bother you again......
Can you explain how you code the function.......i dont quite understand
defun compose (f g)
#'(lambda (&rest args)
(declare (dynamic-extent args))
(funcall f (apply g args))))
thanx
Chris
p/s : a helpful homepage you got there, ihave been there beofre i even post
the message here........good job
Rainer Joswig wrote:
> In article <·················@mail.usask.ca>, ······@mail.usask.ca wrote:
>
> > Hi :
> > thanx for answering my Q
> >
> > the fellowing is my compose function but i dont know if it is right or
> > not..and i am using GCL
>
> How about something like:
>
> (defun compose (f g)
> #'(lambda (&rest args)
> (declare (dynamic-extent args))
> (funcall f (apply g args))))
>
> Example:
>
> (funcall (compose #'first #'second)
> '((1 2) (3 4)))
>
> -> 3
>
> --
> http://www.lavielle.com/~joswig
In article <·················@mail.usask.ca>,
Chris <······@mail.usask.ca> wrote:
>Sorry to bother you again......
>
>Can you explain how you code the function.......i dont quite understand
>
>defun compose (f g)
> #'(lambda (&rest args)
> (declare (dynamic-extent args))
> (funcall f (apply g args))))
What part of it don't you understand? The lambda expression returns a
closure of a function in the environment where F and G are bound to the
functions that were supplied as arguments to COMPOSE. FUNCALL and APPLY
call those F and G bindings.
I'm not trying to be flip, but you'll learn much better if you decompose
the function, try to understand it, and then ask specific questions about
the pieces you don't understand.
--
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.
Sorry ...i am a newbie to lisp.......
>defun compose (f g)
> #'(lambda (&rest args)
> (declare (dynamic-extent args)) <<---------do you think this
link can be skip ?
> (funcall f (apply g args))))
Can you explain what is " &rest " ? .........
(defun f (X) (cdr X))
(defun g (X) (car X))
and if we do a
> (setf (symbol-function 'h) (compose 'g 'f))
how do we know that h only takw 1 arguement ? because we are assignnign the
lambda expression of
compose to h, right ? .....and compse takes 2 arguements...
because i am stuck at where h only takes 1 arguement but compose take 2
arguement..
i am really confused on how they work !
thanx
Chris
Barry Margolin wrote:
> In article <·················@mail.usask.ca>,
> Chris <······@mail.usask.ca> wrote:
> >Sorry to bother you again......
> >
> >Can you explain how you code the function.......i dont quite understand
> >
> >defun compose (f g)
> > #'(lambda (&rest args)
> > (declare (dynamic-extent args))
> > (funcall f (apply g args))))
>
> What part of it don't you understand? The lambda expression returns a
> closure of a function in the environment where F and G are bound to the
> functions that were supplied as arguments to COMPOSE. FUNCALL and APPLY
> call those F and G bindings.
>
> I'm not trying to be flip, but you'll learn much better if you decompose
> the function, try to understand it, and then ask specific questions about
> the pieces you don't understand.
>
> --
> Barry Margolin, ······@bbnplanet.com
> GTE Internetworking, Powered by BBN, Burlington, MA
> *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
> Don't bother cc'ing followups to me.
In article <················@mail.usask.ca>,
Chris <······@mail.usask.ca> wrote:
...
>how do we know that h only takw 1 arguement ? because we are assignnign the
>lambda expression of
>compose to h, right ? .....and compse takes 2 arguements...
>because i am stuck at where h only takes 1 arguement but compose take 2
>arguement..
>i am really confused on how they work !
Let's take two steps back to math class......
Let's say you have a function f(x) and function g(x).
Let's say there is another function defined as:
h(x) = f ( g(x) )
Meaning you pass a value to g whose result is then used as the argument to
f. After passing through both of these functions you produce the same
value h would.
So if g took three arguments. g( x , y , z) then you could "compose"
f and g by
h(x , y , z) = f ( g( x, y, z ) )
So the number of arguments of h is the same as the number of arguments
of g. [ If this is an entry level assignment I would imagine that
it is safe to assume that both f and g will be functions of one
argument. Hence, you don't need the dynamic-extent and &rest wizardy.]
Your function COMPOSE constructs new function given two functions.
That is way it takes two arguments.
P.S. you need the &rest because you don't know in advance how many
arguments g will have. You need the dynamic-extent wizardy
because passing a &rest argument as input to APPLY
can lead to complications that aren't particularly important to the
core of your apparent difficulty.
--
Lyman S. Taylor "The Borg -- party poopers of the Galaxy. "
(·····@cc.gatech.edu) EMH Doctor Star Trek Voyager.
hmm.....i got it now....thanx
so what is " &rest " ? i couldnt finf that in the book.
and how is it associated to the "args " ?
why we dont have to define "args" ?
(defun compose (f g)
#'(lambda (&rest args) <<------wouldnt it mean to take 2 arguements ?
(declare (dynamic-extent args))
(funcall f (apply g args))))
thanx
chris
"Lyman S. Taylor" wrote:
> In article <················@mail.usask.ca>,
> Chris <······@mail.usask.ca> wrote:
> ...
> >how do we know that h only takw 1 arguement ? because we are assignnign the
> >lambda expression of
> >compose to h, right ? .....and compse takes 2 arguements...
> >because i am stuck at where h only takes 1 arguement but compose take 2
> >arguement..
> >i am really confused on how they work !
>
> Let's take two steps back to math class......
>
> Let's say you have a function f(x) and function g(x).
>
> Let's say there is another function defined as:
>
> h(x) = f ( g(x) )
>
> Meaning you pass a value to g whose result is then used as the argument to
> f. After passing through both of these functions you produce the same
> value h would.
>
> So if g took three arguments. g( x , y , z) then you could "compose"
> f and g by
>
> h(x , y , z) = f ( g( x, y, z ) )
>
> So the number of arguments of h is the same as the number of arguments
> of g. [ If this is an entry level assignment I would imagine that
> it is safe to assume that both f and g will be functions of one
> argument. Hence, you don't need the dynamic-extent and &rest wizardy.]
>
> Your function COMPOSE constructs new function given two functions.
> That is way it takes two arguments.
>
> P.S. you need the &rest because you don't know in advance how many
> arguments g will have. You need the dynamic-extent wizardy
> because passing a &rest argument as input to APPLY
> can lead to complications that aren't particularly important to the
> core of your apparent difficulty.
>
>
>
> --
>
> Lyman S. Taylor "The Borg -- party poopers of the Galaxy. "
> (·····@cc.gatech.edu) EMH Doctor Star Trek Voyager.
[ Could you please avoid quoting the entire article you followup to?
--Thanks ]
Chris <······@mail.usask.ca> writes:
> so what is " &rest " ? i couldnt finf that in the book.
Throw the book away. &rest is used to declare a variable number of
arguments; e.g.:
(defun foo (x y &rest other-args)
...)
Then FOO will be a function taking at least two arguments. When you
call the function, the first two arguments will be available through X
and Y, and the rest will be stored in OTHER-ARGS, as a list.
and why we dont have to define "args" ?
thanx
Chris
Barry Margolin wrote:
> In article <·················@mail.usask.ca>,
> Chris <······@mail.usask.ca> wrote:
> >Sorry to bother you again......
> >
> >Can you explain how you code the function.......i dont quite understand
> >
> >defun compose (f g)
> > #'(lambda (&rest args)
> > (declare (dynamic-extent args))
> > (funcall f (apply g args))))
>
> What part of it don't you understand? The lambda expression returns a
> closure of a function in the environment where F and G are bound to the
> functions that were supplied as arguments to COMPOSE. FUNCALL and APPLY
> call those F and G bindings.
>
> I'm not trying to be flip, but you'll learn much better if you decompose
> the function, try to understand it, and then ask specific questions about
> the pieces you don't understand.
>
> --
> Barry Margolin, ······@bbnplanet.com
> GTE Internetworking, Powered by BBN, Burlington, MA
> *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
> Don't bother cc'ing followups to me.
In article <·················@mail.usask.ca>,
Chris <······@mail.usask.ca> wrote:
>and why we dont have to define "args" ?
What is this horrible Common Lisp book you're using, that doesn't even
explain &rest or lambda expressions?
ARGS is the name of the arguments to the function that COMPOSE returns.
Putting it in the argument list after LAMBDA defines it.
--
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.
defun compose (f g)
> > #'(lambda (&rest args) <--------it (&rest) group all the arguements
into a list ?
> > (declare (dynamic-extent args))
> > (funcall f (apply g args))))
I thought the ARGS is the arguement that we passed it ?
Isn`t it ?
Its a really old book which i borrowed from the library
it was published in 1987 called "Essential Lisp "
( i dont think you really want to know about it *^_^*)
I dont know which book is the right one !
Do you have any suggestion ?
thanx
Chris
Barry Margolin wrote:
> In article <·················@mail.usask.ca>,
> Chris <······@mail.usask.ca> wrote:
> >and why we dont have to define "args" ?
>
> What is this horrible Common Lisp book you're using, that doesn't even
> explain &rest or lambda expressions?
>
> ARGS is the name of the arguments to the function that COMPOSE returns.
> Putting it in the argument list after LAMBDA defines it.
>
> --
> Barry Margolin, ······@bbnplanet.com
> GTE Internetworking, Powered by BBN, Burlington, MA
> *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
> Don't bother cc'ing followups to me.
In article <·················@mail.usask.ca>,
Chris <······@mail.usask.ca> wrote:
>defun compose (f g)
>> > #'(lambda (&rest args) <--------it (&rest) group all the arguements
>into a list ?
>> > (declare (dynamic-extent args))
>> > (funcall f (apply g args))))
>
>I thought the ARGS is the arguement that we passed it ?
>Isn`t it ?
F and G are the arguments we passed to COMPOSE. COMPOSE returns a new
function (the one that calls F and G), and ARGS is the arguments that are
passed to this new function, and which will then be passed on to G.
>I dont know which book is the right one !
>Do you have any suggestion ?
The FAQ has references to a number of books.
--
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.
In article <·················@mail.usask.ca>,
Chris <······@mail.usask.ca> wrote:
...
>I dont know which book is the right one !
>Do you have any suggestion ?
There is no one correct answer. If your library has any of the following
use them instead:
ANSI Common Lisp Paul Graham
CommonLisp: A Gentle Introduction to Symbolic Computing David Touretzky
CommonLISPcraft Robert Wilensky
Lisp 3rd edition Patrick Winston and Berthold Horn
Some reviews on the above
http://www.elwood.com/alu/table/books.htm
P.S. If you are not wedded to using common lisp then another approach would be
to see if the library has a copy of:
Structure and Interpretation of Computer Program Ableson and Sussman
and pick a Scheme implementation to use.
--
Lyman S. Taylor "Computers are too reliable to replace
(·····@cc.gatech.edu) humans effectively."
Commander Nathan Spring, "Starcops"
defun compose (f g)
#'(lambda (&rest args)
(declare (dynamic-extent args))
(funcall f (apply g args)))) <---------cant we use "FUNCALL " instead of
"APPLY" ?
why ?
thanx
Chris
Barry Margolin wrote:
> In article <·················@mail.usask.ca>,
> Chris <······@mail.usask.ca> wrote:
> >and why we dont have to define "args" ?
>
> What is this horrible Common Lisp book you're using, that doesn't even
> explain &rest or lambda expressions?
>
> ARGS is the name of the arguments to the function that COMPOSE returns.
> Putting it in the argument list after LAMBDA defines it.
>
> --
> Barry Margolin, ······@bbnplanet.com
> GTE Internetworking, Powered by BBN, Burlington, MA
> *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
> Don't bother cc'ing followups to me.
In article <·················@mail.usask.ca>,
Chris <······@mail.usask.ca> wrote:
> (funcall f (apply g args)))) <---------cant we use "FUNCALL " instead of
>"APPLY" ?
if g were restricted to one argument you could.
>why ?
Two rules of thumb:
FUNCALL -- when you have a fix argument sequence you want to invoke a
function on. You know the number of arguments at the time
you write the FUNCALL expression.
APPLY -- when you don't know in advance how many arguments the
function you want to invoke will be passed. The last argument
to APPLY must be a list.
Note the descriptions of the arguments and examples for both:
http://www.harlequin.com/education/books/HyperSpec/Body/fun_funcall.html#funcall
http://www.harlequin.com/education/books/HyperSpec/Body/fun_apply.html#apply
--
Lyman S. Taylor "Computers are too reliable to replace
(·····@cc.gatech.edu) humans effectively."
Commander Nathan Spring, "Starcops"
······@lavielle.com (Rainer Joswig) writes:
>
> (defun compose (f g)
> #'(lambda (&rest args)
> (declare (dynamic-extent args))
> (funcall f (apply g args))))
>
>
(this is what I use, but without the declare...) )
I was thinking the other day that it would be nice to have version of
compose that takes any number of functions and strings them all
together on the assumption that the values returned by each function
are compatible with the lambda list of its predecessor.
I guess this should work?
;;;warning: untested code;;;;
(defun compose2fns (f1 f2)
#'(lambda (&rest args) (funcall f1 (apply f2 args))))
(defun compose (&rest fns)
(reduce #'compose2fns fns
:initial-value #'(lambda (&rest args)
(values-list args))
:from-end t))
In article <·················@mail.usask.ca>, ······@mail.usask.ca wrote:
> just want to thank you all for your time and your knowledge
> i have just learn somthing that i might not know by going to the class
>
>
> THANK YOU
Thank you for trying to understand.
--
http://www.lavielle.com/~joswig