1
votes

I am making this Exercises from this page http://www.htdp.org/2003-09-26/Solutions/natnum-list.html

In this code

(define (depth a-dl
(cond
[(symbol? a-dl) 0]
[else (add1 (depth (first a-dl)))]))

We use add1 predefined function which means (+ x 1), I want to swap add1 with (+ x 1) or with lambda. Is it possible? if yes how?

Butt I want not write so outside the function.

(define (add1 x) 
        (+ x 1))

or so

(define add1 
(lambda (x)
    (+ x 1)))
1
You can always replace one function with another that does the same or substitute the entire call with the expression that does the same. What do you mean by how? - Sylwester
I mean for this code segment <pre>(add1 (depth (first a-dl)) <code> I can't use <pre>( ((+ a-dl 1) (depth (first a-dl)) <code> - Memo
No you either replace (add1 (depth...)) with ((lambda (x) (+ x 1)) (depth...)) or replace the whole call (+ (depth...) 1). These are true refactorings of your code - Sylwester

1 Answers

1
votes

add1 can not mean (+ x 1), because there's no x in add1.

(add1 x) can mean (+ x 1) if it so defined.

To make it have this meaning, we'd define it as

(define (add1 x)
        (+  1 x))

or equivalently as

(define  add1
     (lambda (x)
        (+  1 x)))

this means that wherever add1 appears, (lambda (x) (+ 1 x)) can appear instead, with the same effect.

Of course, writing

( (lambda (x) (+ 1 x)) ....Y.... )

is the same as writing

(              + 1     ....Y.... )