Hi,

sqrtsben hat geschrieben:just wanted to touch base on the CPS task 8.1. Does this make sense?

Code: Alles auswählen

```
(define (remove/k item l k)
(cond [(empty? l) (k empty)]
[(not (list? l)) (k empty)]
[else
(if (equal? item (first l))
(remove/k item (rest l) (lambda(list-rest)
(k list-rest)))
(remove/k item (rest l) (lambda(list-rest)
(k (cons (first l) list-rest))))
)]))
```

I think it's correct, although on equal you could just use

*(remove/k item (rest l) k)*. (And, besides, the

*[(not (list? l)) (k empty)]* check deviates from the original function, this wasn't checked at all there...)

For 4.1, the question is how the memoization works exactly. Does the interpreter cache the result of the "function call" to + - that would mean for the two {f {+ 1 1}}, it would only run the calculation once. Same holds true for the two calls to f(2). That would mean, at this point the interpreter would only have done two additions, now followed by the third one, as it doesn't yet "know" what (+ 4 4) is. In total, that would result in 3 additions.

Good point; I assumed that only user-defined functions use memoization and that + does not.

For call-by-name, I agree with you. For call-by-need, the question is whether (+1 1) is an "argument" or not. If it is, (+ 1 1) would have to be calculated just once. From there one, we need to run f(2) twice as we only cache arguments, not function values for certain arguments. Finally, the outer addition leads to 4 additions.

I don't get this, what do you mean by "is (+ 1 1) an 'argument'"? ... It's an argument to f (in both calls), so it gets only evaluated once in each f call (= 2 times overall). Then each f does an addition (= 2), finall the results are added (= 1), in sum: 5. The only thing, along with your first comment, would be, whether "(f (+ 1 1))" is seen as an argument to + and thus only one f is called... but again, I though built-in functions were left out in this consideration.

sqrtsben hat geschrieben:btw - anyone willing to provide a solution to 9.1. I'm completely stuck on that one

Sure. The idea is to use that a list of length l calls the "c" function (in l-cons) l times before returning the "n" value. So one can pass a function for c which just ignores the actual list element "e" and simply calls a provided function f on the rest. Now if one pass the parameter x of the church number function as the n, the result is that f is applied l-times to x, i.e., you get the function representing the number l.

Now the code:

Code: Alles auswählen

```
(define (l-length l)
(lambda (f)
(lambda (x)
(l (lambda (e r) (f r)) x))))
```

Best,

Felix