Will
Will

Reputation: 153

How to check if two functions are equal in Racket?

In trying to traverse a list like this, '((a + b) + (c + d)), I'll land on the identifier, +, and not know how to verify that it is a plus sign and not some other function like / or *.

How Do I verify that the plus signs I encounter are in fact plus signs?

Upvotes: 1

Views: 804

Answers (1)

Sylwester
Sylwester

Reputation: 48745

First off. + in (a + b) are symbols, not functions. This is evident if you ever evaluate +, which is a variable, you will be presented the value behind the variable. If you evaluate '+ which evaluates to a symbol it prints out a symbol.

So your real question is how to check if one symbol is the same as another. A symbol in Scheme is unqiue and thus if two symbols look the same they are the same symbol. You may use eq? to check this:

(define plus '+)
(eq? plus '+) ; ==> #t
(eq? plus '-) ; ==> #f

Upvotes: 2

Related Questions