Scheme problem (using a function as a parameter) -


i'm scheme newbie , trying make sense of homework. i've function made earlier called duplicate, , looks this:

( define ( duplicate lis )           (if (null? lis) '())           ((cons (car lis) (cons (car lis) (duplicate (cdr lis))))          )) 

a typical i/o i: (duplicate '(1 2 3 4)) o: (1 1 2 2 3 3 4 4), basicly duplicates in list. moving on: i'm supposed make function that's called comp. it's supposed built this:

(define (comp f g) (lambda (x) (f (g (x)))) 

where input '(1 2 3 4) , return (1 1 4 4 9 9 16 16)

so f = duplicate , g = lambda. know lambda should this:

(lambda (x) (* x x)) 

but here's problem starts, i've spent several hours on this, , can see not made progress.

any appreciated. best regards.

use map:

> (map (lambda (x) (* x x)) (duplicate '(1 2 3 4))) => (1 1 4 4 9 9 16 16) 

or, modify duplicate take procedure second argument , apply each element of list:

(define (duplicate lst p)   (if (null? lst) ()       (append (list (p (car lst)) (p (car lst))) (duplicate (cdr lst) p))))  > (duplicate '(1 2 3 4) (lambda (x) (* x x))) => (1 1 4 4 9 9 16 16) 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -