Jelte Deproft
Jelte Deproft

Reputation: 21

Scheme : using lambda as a parameter

hy everyone, for school i have to make a function where lambda is used as a parameter

like so : (string (lambda ...) 5 40) where we have to fill in the dots

this is the function we had to reinvent, the regular string version

(define (string decoration n r)            >string decoration is a function that creates a string with either fish or pumpkins hanging on the string
  (define (decorations k)                  >decorations is the recursive function which hangs all the decorations together
    (if (= k 1)
        (decoration r 10)                  > here decoration is to be replaced with either a pumpkin or a fish as stated in the parameters
        (ht-append (decoration r 10)       > ht-append is a function that appends 2 figures Horizontally at the Top
                   (decorations (- k 1)))))
  (hang-by-thread (decorations n)))        > hang by thread is a function that hangs all the decorations at a string

all the names should be self-explanatory, the function takes a decoration , either a fish or a pumpkin and hangs it by a thread. But the fish has 3 parameters and the pumpkin has 2 which caused an error. So in a previous exercise we had to make an extra definition called fish-square which uses only 2 parameters to make a fish. Now we have to implement this same squared fish but with a lambda. Any help is greatly appreciated

(define (fish-square wh l)                        > this is the fish square functio which makes a fish but with 2 times the same parameter so it looks like a square
  (vc-append (filled-rectangle 2 l) (fish wh wh)))   > the l is the length of the string that attaches the fish to the string at the top

the fish function is just (fish x y) x makes it longer, y makes it taller. the pumpkin function is just (pumpkin x y) same story

so my question is, how do rewrite the given code , but with lambda as a parameter. i would upload an image, but my repuation isn't high enough :s

Upvotes: 0

Views: 151

Answers (2)

Sylwester
Sylwester

Reputation: 48745

Imagine you have the procedure +. It could be any really. It takes several arguments but you need a different procedure that takes one and adds that to an already constant value 3.

Thus you want to pass + with the extra information that it should add 3.

A full definition of such procedure would be

(define (add3 n)
   (+ 3 n))

which is the short form of the full define

(define add3 
   (lambda (n)
      (+ 3 n)))

Now when passing a procedure 3+ you could actually just pass it's definition. These two does the same:

(do-computation add3 data)

(do-computation (lambda (n) (+ 3 n)) data)

Upvotes: 0

Óscar López
Óscar López

Reputation: 236004

The string procedure as it is already receiving a procedure as a parameter (you don't have to rewrite it!), decoration can be any two-argument function used for decorating. Now when you call it you can pass a named procedure, for example:

(define (decoration r n)
  <body>)

(string decoration
        5
        40)

... Or just as easily, you can pass the same procedure in-line as a lambda, and if I understood correctly, this is what you're supposed to do:

(string (lambda (r n)
          <body>)
        5
        40)

Just replace <body> with the actual body of the decoration you want to use. In othre words: the change you're expected to do is in the way you pass the parameters to the function at invocation time, but you're not expected to change the function itself.

Upvotes: 2

Related Questions