Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheme - eq? compare between 2 strings?

Tags:

scheme

I have a problem in my program.

I have a condition that compare between 2 string:

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

When exp1 give me a string, and exp2 give me a string. To be sure, when I change the "eq?" to "=", it give me the next problem:

=: expects type <number> as 2nd argument, given: ie; other arguments were: ie.

When I'm running the program, the function doesnt enter to the first expression in the "if" function, and enter to the second one (meaning the condition is false).

What can I do?

Thank you.

like image 624
Tom Avatar asked Apr 20 '11 12:04

Tom


People also ask

How do you compare two strings in scheme?

[Function]string= compares two strings and is true if they are the same (corresponding characters are identical) but is false if they are not. The function equal calls string= if applied to two strings. The keyword arguments :start1 and :start2 are the places in the strings to start the comparison.

Can you use == to compare two strings?

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

How do you compare two string objects for their equality?

Using String. equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If any character does not match, then it returns false.

Can we compare two strings using comparison operators?

You use the relational (comparison) operators =, <>, ><, <, <=, =<, >, >=, and => to ascertain the relative positions of two strings in ASCII sort order. The result of comparing two strings in this way is a value of True, False, or NULL (if one of the operands is NULL).


1 Answers

According to the Equivalence predicates section of R6RS, you should be using equal?, not eq?, which instead tests whether its two 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, the Strings section also mentions string=?, specifically for string comparisons, which probably avoids doing a type check.

like image 169
dfan Avatar answered Oct 06 '22 08:10

dfan