On Saturday, January 12, 2019 8:34:35 PM PST Hassan Shahin wrote:
> I have this definition for a procedure:
> 
> (define type-of (lambda (item)
>                          (cond
>                            [(pair? item) 'pair]
>                            [(null? item) 'empty-list]
>                            [(number? item) 'number]
>                            [(symbol? item) 'symbol]
>                            [else 'some-other-type])))
> 
> My understanding is that if the first 4 conditions fail (=> #f
> <https://plus.google.com/s/%23f/posts>), then the last expression (the else
> expression) is evaluated.
> When I apply this procedure to John, as in (type-of John) I get an error
> (; john: undefined; ; cannot reference an identifier before its definition)
> .
> 
> What is going on?
> Thanks

The issue is that John is evaluated before it is passed to type-of. For 
example, the expression (type-of (+ 2 3)) is equivalent to (type-of 5), 
because the expression (+ 2 3) is evaluated to 5 before it is supplied to the 
type-of function.

In the Google+ post you write "cond behaves as if (not evaluating its 
arguments)". It is true that the arguments to cond, i.e. [(pair? item) 'pair], 
[(null? item) 'empty-list], and so on are not immediately evaluated. However, 
item is evaluated before it is passed to type-of, hence the "cannot reference" 
error.

See https://docs.racket-lang.org/reference/if.html and 
https://docs.racket-lang.org/reference/eval-model.html?q=evaluation%20model for 
more information 
about how Racket evaluates expressions.


-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to