Author 
Message 
Karl M. Hegbloo #1 / 21

I'm stuck: composition of functions
I'm studying Scheme on my own, at home, with 'Scheme and the Art of Programming' by George Springer and Daniel P. Friedman. I'm just getting into the second section, on returning closures from functions... The exercise I cannot get is #7.3, composemany. What am I doing wrong? ;;; ;;; Exercises pg. 206 ;; Program 7.8 (define compose (lambda (f g) (lambda (x) (f (g (x)))))) ;;; 7.2 (define compose3 (lambda (f g h) (lambda (x) (f (g (h x)))))))) ;; 7.3 (define composemany (lambda funcs (letrec ((buildexpression (lambda ls (cond ((null? (cdr ls)) (list (car ls) 'x)) (else (cons (car ls) (list (apply buildexpression (cdr ls))))))))) (lambda (x) (apply buildexpression funcs))))) 
http://www.***.com/ ~karlheg (K0D) AYGGE01 Portland, OR, USA :) Proudly running Linux 2.0.21 and GNU public software!

Mon, 12 Apr 1999 03:00:00 GMT 


Jason Nordwi #2 / 21

I'm stuck: composition of functions
: (define composemany : (lambda funcs : (letrec ((buildexpression : (lambda ls : (cond : ((null? (cdr ls)) (list (car ls) 'x)) : (else (cons (car ls) : (list (apply buildexpression (cdr ls))))))))) : (lambda (x) (apply buildexpression funcs))))) my first question is why the letrec with only a single expression? ok... here is the first problem that so you test for (cdr ls) to be null and then you fix that problem by basically appending 'x to it... but then you just returning it... are you trying to say call call the (car ls) on x? if so the syntax is ((car ls) x) or more thouroughly if you want it delayed in the evaluation (lambda (x) ((car ls) x))... i am also unclear as to what the else statement does... the cons has completely lost me... i think again that you want to use an apply or lambda... you should relook at the sections on apply... or just try to solve the problem more simply... use lambda, too... if in doubt... sprinkle a few more lambda throught the problem and it will at least look cooler... once you understand your own solution... take a look at mine.. i think that it is fairly straightforward... luck. (define (composemany . args) (define (comploop newfn fnlist) (if (null? fnlist) newfn (comploop (lambda (x) ((car fnlist) (newfn x))) (cdr fnlist)))) (comploop (lambda (x) x) (reverse args))) jay

Tue, 13 Apr 1999 03:00:00 GMT 


Seth Tis #3 / 21

I'm stuck: composition of functions
Quote: >(define compose > (lambda (f g) > (lambda (x) > (f (g (x))))))
In (f (g (x))), think about what (x) means. Quote: >(define composemany > (lambda funcs > (letrec ((buildexpression > (lambda ls > (cond > ((null? (cdr ls)) (list (car ls) 'x)) > (else (cons (car ls) > (list (apply buildexpression (cdr ls))))))))) > (lambda (x) (apply buildexpression funcs)))))
You're going about this somewhat the wrong way. You shouldn't build an expression (a list containing Lisp code): you should build a function directly, like your compose and compose3 functions do. There's a very simple recursive definition of composemany. Consider: (compose f g h ...) = (lambda (x) (f ((compose g h ...) x))) 

Tue, 13 Apr 1999 03:00:00 GMT 


Jason Nordwi #4 / 21

I'm stuck: composition of functions
) There's a very simple recursive definition of composemany. ) Consider: (compose f g h ...) = (lambda (x) (f ((compose g h ...) x))) hmmm... how does this build the _full_ function directly... you still have delayed calls to compose.. which is still just an expression. jay

Tue, 13 Apr 1999 03:00:00 GMT 


Karl M. Hegbloo #5 / 21

I'm stuck: composition of functions
(define (composemany2 . fns) (cond ((null? fns) (lambda (x) x)) (else (lambda (x) ((car fns) ((apply compose (cdr fns)) x)))))) Thanks for the assistance. If I study this every day, it will seem very natural to me after a while. ;) 
http://www.teleport.com/~karlheg (K0D) AYGGE01 Portland, OR, USA :) Proudly running Linux 2.0.22 and GNU public software!

Fri, 16 Apr 1999 03:00:00 GMT 


Peter Drak #6 / 21

I'm stuck: composition of functions
Quote: > I'm studying Scheme on my own, at home, with 'Scheme and the Art of > Programming' by George Springer and Daniel P. Friedman. I'm just > getting into the second section, on returning closures from functions... > The exercise I cannot get is #7.3, composemany. What am I doing > wrong? > ;; 7.3 > (define composemany > (lambda funcs > (letrec ((buildexpression > (lambda ls > (cond > ((null? (cdr ls)) (list (car ls) 'x)) > (else (cons (car ls) > (list (apply buildexpression (cdr ls))))))))) > (lambda (x) (apply buildexpression funcs)))))
The problem is that you're building a list of closures, and you can't apply a list. Also, I think you want parens around the argument ls in the definition of buildexpression. Notice that compose could also be written as: (define compose (lambda (f g) (lambda (x) (let ((rest (lambda (x) (g x)))) (f (rest x)))))) Hope this helps,  Peter Drake, PhD Student in Cognitive Science, IU Bloomington
"Mathematics compares the most diverse phenomena and discovers the secret analogies that unite them."  Joseph Fourier

Sun, 18 Apr 1999 03:00:00 GMT 


Jason Nordwi #7 / 21

I'm stuck: composition of functions
: > (define composemany : > (lambda funcs : > (letrec ((buildexpression : > (lambda ls : > (cond : > ((null? (cdr ls)) (list (car ls) 'x)) : > (else (cons (car ls) : > (list (apply buildexpression (cdr ls))))))))) : > (lambda (x) (apply buildexpression funcs))))) : : The problem is that you're building a list of closures, and you can't : apply a list. Also, I think you want parens around the argument ls in : the definition of buildexpression. hmm... i thought that only that you could apply was a list... ? and that lack of parens around the parameter "ls" is correct usage (although the proecdure doesnt quite work as planned :) it signifies that "ls" is always to be a list of values. jay

Sun, 18 Apr 1999 03:00:00 GMT 


Bjorn Kvam #8 / 21

I'm stuck: composition of functions
How about (define compose (lambda lst (letrec ((help (lambda (ls) (if ls (lambda(x)((car ls)((help(cdr ls))x))) (lambda(x)x) )))) (help lst) ))) compose composes functions of arity 1. call it as follows ((compose fun1 fun2 fun3) arg) which, apart from side effects, produces the same result as (fun1 (fun2 (fun3 arg))) Jacob  J. J. A. Koot Stichting Academisch Rekencentrum Amsterdam Tel: +31 20 5923019 Postbus 94613, 1090 GP, Amsterdam, Netherlands

Sun, 18 Apr 1999 03:00:00 GMT 


Jason Nordwi #9 / 21

I'm stuck: composition of functions
: How about [rest of code cut] : (if ls ummm... '() <> #f... correct?

Mon, 19 Apr 1999 03:00:00 GMT 


J.J.A. Koo #10 / 21

I'm stuck: composition of functions
How about (define compose (lambda lst (letrec ((help (lambda (ls) (if ls (lambda(x)((car ls)((help(cdr ls))x))) (lambda(x)x) )))) (help lst) ))) compose composes functions of arity 1. call it as follows ((compose fun1 fun2 fun3) arg) which, apart from side effects, produces the same result as (fun1 (fun2 (fun3 arg))) Jacob  J. J. A. Koot Stichting Academisch Rekencentrum Amsterdam Tel: +31 20 5923019 Postbus 94613, 1090 GP, Amsterdam, Netherlands

Mon, 19 Apr 1999 03:00:00 GMT 


J.J.A. Koo #11 / 21

I'm stuck: composition of functions
Quote:
> ummm... '() <> #f... correct?
As far as I know: (if '() 'a 'b) ==> b But if you want you can replace 'if ls' by 'if (not (null? ls))' or better: (define compose (lambda lst (letrec ((help (lambda (ls) (if (null? ls)(lambda(x)x) (lambda(x)((car ls)((help(cdr ls))x))) )))) (help lst) ))) jacob

Tue, 20 Apr 1999 03:00:00 GMT 


Rob Warno #12 / 21

I'm stuck: composition of functions
+  As far as I know: (if '() 'a 'b) ==> b + IEEE Scheme requires (if '() 'a 'b) ==> a But in R4RS it says, "It is no longer specified whether the empty list counts as true of false in conditional expressions." (But they then implicitly [to my reading] encourage "true" by reminding the reader about IEEE Scheme.) In any case, implementations do vary, so using "null?" is always the safest thing. Rob 
Silicon Graphics, Inc. http://reality.sgi.com/rpw3/ 2011 N. Shoreline Blvd. Phone: 4159331673 FAX: 4159330979 Mountain View, CA 94043 PPASELIA

Thu, 22 Apr 1999 03:00:00 GMT 


Jason Nordwi #13 / 21

I'm stuck: composition of functions
: But in R4RS it says, "It is no longer specified whether the empty list : counts as true of false in conditional expressions." (But they then : implicitly [to my reading] encourage "true" by reminding the reader : about IEEE Scheme.) hmmm.. this doesnt appear to be true: it seems like it is more explicit than anything else saying that '() => #f here's the cut: Of all the standard Scheme values, only #f counts as false in conditional expressions. Except for #f, all standard Scheme values, including #t, pairs, the empty list, symbols, numbers, strings, vectors, and procedures, count as true. : In any case, implementations do vary, so using "null?" is always the : safest thing. agreed.   Jay Nordwick

Thu, 22 Apr 1999 03:00:00 GMT 

