Scheme - equiv. compare between two lines?

I have a problem in my program.

I have a condition for comparing two strings:

(if (eq? (exp1) (exp2)))

When exp1 give me a string and exp2 give me a string. Of course, when do I change eq? to "=", this gives me the following problem:

=: expects type <number> as the 2nd argument, i.e. Other arguments were: i.e.

When I run the program, the function doesnt enter the first expression into the "if" function and enter the second (this means that this condition is false).

What can I do?

Thanks.

+4
source share
2 answers

According to the Equivalence predicates section in R6RS, should you use equal? , but not eq? which checks instead, the arguments are exactly the same object (not two objects with the same value).

 (eq? "a" "a") ; unspecified (equal? "abc" "abc") ; #t 

As knivil notes in a comment, does the Strings section also mention string=? in particular to compare strings, which probably avoids type checking.

+4
source

I wrote a little helper function for this problem.

 ; test if eq? (define == (lambda (xy) (if (and (string? x) (string? y)) (string=? xy) (if (or (string? x) (string? y)) (= 1 0) ;return false (equal? xy))))) (define a "aString") (define l '("aString" "aOtherString")) (== (car l) a) ; true (== 1 1) ; true (== 1 0) ; false (== "a" 1) ; false diff. type (== "a" "b") ; false (== "a" "a") ; true (== '("a" "b") '("a" "b")) 
0
source

All Articles