kaminey
kaminey

Reputation: 23

Evaluating Racket expression

Given these definitions:

(define s 8)
(define p (/ s 2))
(define (f s p)
        (cond [(or (> s 0) p) 'yes] [(< s 0) 'no])) 

I want to evaluate this expression:

(f 0 (and (< s p) (> s 2))) 

So far I have:

       ⇒    (f 0 (and (< 8 p) (> s 2))) 
       ⇒    (f 0 (and (< 8 4) (> s 2))) 
       ⇒    (f 0 (and false (> s 2)))
       ⇒    (f 0 false)

How do I finish this?

Upvotes: 0

Views: 142

Answers (2)

Sylwester
Sylwester

Reputation: 48745

Racket sees that f is a procedure and evaluates it's arguments:

(f 0 (and (< s p) (> s 2))) ; ==>
(f 0 (and (< 8 4) (> 8 2))) ; ==>
(f 0 (and #f #t))           ; ==>
(f 0 #f)                    ; s=0, p=#f

Then the arguments is substituted for the variables in the body of f:

; ==>
(cond [(or (> 0 0) #f) 'yes]
      [(< 0 0) 'no])

since both (or (> 0 0) #f) and (< 0 0) are #f the result is the undefined value chosen by the implementation. In racket it's #<void>

; ==>
#<void>

This could have been avoided by always have a else term:

(define (f s p)
  (cond [(or (> s 0) p) 'yes] 
        [(< s 0) 'no]
        [else 'banana]))

(f 0 (and (< s p) (> s 2))) ; ==> banana

Upvotes: 0

Scott Hunter
Scott Hunter

Reputation: 49803

You need to replace this with the body of f (the cond expression) with the parameters replaced by their matching arguments.

Upvotes: 1

Related Questions