I'm stuck: composition of functions
Author Message
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, compose-many.  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 compose-many
(lambda funcs
(letrec ((build-expression
(lambda ls
(cond
((null? (cdr ls)) (list (car ls) 'x))
(else (cons (car ls)
(list (apply build-expression (cdr ls)))))))))
(lambda (x) (apply build-expression funcs)))))

--

http://www.*-*-*.com/ ~karlheg
(K0D) AYG-GE01  Portland, OR, USA
:) Proudly running Linux 2.0.21 and GNU public software!

Mon, 12 Apr 1999 03:00:00 GMT
I'm stuck: composition of functions

: (define compose-many
:  (lambda funcs
:   (letrec ((build-expression
:    (lambda ls
:     (cond
:      ((null? (cdr ls)) (list (car ls) 'x))
:      (else (cons (car ls)
:       (list (apply build-expression (cdr ls)))))))))
:  (lambda (x) (apply build-expression 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 (compose-many . args)
(define (comp-loop new-fn fn-list)
(if (null? fn-list)
new-fn
(comp-loop (lambda (x) ((car fn-list) (new-fn x))) (cdr fn-list))))
(comp-loop (lambda (x) x) (reverse args)))

jay

Tue, 13 Apr 1999 03:00:00 GMT
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 compose-many
>  (lambda funcs
>    (letrec ((build-expression
>          (lambda ls
>            (cond
>             ((null? (cdr ls)) (list (car ls) 'x))
>             (else (cons (car ls)
>                         (list (apply build-expression (cdr ls)))))))))
>      (lambda (x) (apply build-expression funcs)))))

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 compose-many.
Consider:  (compose f g h ...) = (lambda (x) (f ((compose g h ...) x)))
--

Tue, 13 Apr 1999 03:00:00 GMT
I'm stuck: composition of functions

) There's a very simple recursive definition of compose-many.
) 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
I'm stuck: composition of functions

(define (compose-many2 . 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) AYG-GE01  Portland, OR, USA
:) Proudly running Linux 2.0.22 and GNU public software!

Fri, 16 Apr 1999 03:00:00 GMT
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, compose-many.  What am I doing
> wrong?
> ;; 7.3
> (define compose-many
>   (lambda funcs
>     (letrec ((build-expression
>          (lambda ls
>            (cond
>             ((null? (cdr ls)) (list (car ls) 'x))
>             (else (cons (car ls)
>                         (list (apply build-expression (cdr ls)))))))))
>       (lambda (x) (apply build-expression 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 build-expression.  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
I'm stuck: composition of functions

: > (define compose-many
: >   (lambda funcs
: >     (letrec ((build-expression
: >        (lambda ls
: >          (cond
: >           ((null? (cdr ls)) (list (car ls) 'x))
: >           (else (cons (car ls)
: >                       (list (apply build-expression (cdr ls)))))))))
: >       (lambda (x) (apply build-expression 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 build-expression.

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
I'm stuck: composition of functions

(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
I'm stuck: composition of functions

[rest of code cut]
:       (if ls

ummm... '() <> #f... correct?

Mon, 19 Apr 1999 03:00:00 GMT
I'm stuck: composition of functions

(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
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
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

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: 415-933-1673  FAX: 415-933-0979
Mountain View, CA  94043        PP-ASEL-IA

Thu, 22 Apr 1999 03:00:00 GMT
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

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

 Page 1 of 2 [ 21 post ] Go to page: [1] [2]

Relevant Pages