You are here

Case and string?

I'm having trouble figuring out how to evaluate a string using a TinyScheme (case ) construct. I am consistently getting wrong results.

In the 3 attached test cases (no pun intended), I try different tacks to get the case to match arbitrary strings.

The goal is to return a results string.

In the first file, I code case as follows:

(case (substring testString 0 3)
  ("RGB" "Found RGB")
  ("CW-" "Found CW-")
  ("CCW" "Found CCW")
  (else (string-append "Didn't find anything in '" (substring testString 0 3) "'."))
)

Running test case 1 returns "Found RGB" no matter what I use for the test string.

In the second file:

(case (substring testString 0 3)
  (("RGB") "Found RGB")
  (("CW-") "Found CW-")
  (("CCW") "Found CCW")
  (else (string-append "Didn't find anything in '" (substring testString 0 3) "'."))
)

Running test case 2 returns "Didn't find anything in '[first 3 chars of the input]'." no matter what I use for the test string.

In the third file, based on some Scheme code I found with a Google search:

(case (string->symbol (substring testString 0 3))
  (('"RGB") "Found RGB")
  (('"CW-") "Found CW-")
  (('"CCW") "Found CCW")
  (else (string-append "Didn't find anything in '" (substring testString 0 3) "'."))
)

Running test case 3 also returns "Didn't find anything in '[first 3 chars of the input]'." no matter what I use for the test string.

Is there a way to accomplish string compare via (case ), or will I need to do this using (if ) statements? (I have multiple sets of strings to check for, several of them more than 3 items, so I'd like to use (case ) if it's doable.

Case uses eqv? predicate. The eqv? procedure returns #f if:.... * obj1 and obj2 are pairs, vectors, strings, bit strings, records, cells, or weak pairs that denote distinct locations. In your code, the result of the substring and the string literals are not the same object and so not equivalent (eqv?) At the console: > (eqv? "RGB" (substring "RGBA" 0 3)) #f I just looked up this on the web, didn't know it before. Give in, use a wordy construct? Scheme is elegant for theorists, for practitioners like me it is highly unreadable. plashless, off banks of noon

So if I put into the console: > (eqv? (string->symbol "RGB") (string->symbol (substring "RGBA" 0 3))) #t > (eqv? (string->symbol "RGB") (string->symbol (substring "RABA" 0 3))) #f but it still doesn't work in the case statement. Test case 4 added.

my you are persistent from: http://groups.csail.mit.edu/mac/ftpdir/scheme-7.4/doc-html/scheme_3.html... >>>special form: case key clause clause ... Key may be any expression. Each clause has this form: ((object ...) expression expression ...) No object is evaluated, and all the objects must be distinct. The last clause may be an else clause, which has the form: (else expression expression ...) <<< In other, fuzzy, might be wrong, my words: the cases are not evaluated (executed) so your (string->symbol "RGB") is not evaluated. What exactly it is (a literal, a symbol, ....) I could not say. Again, I am not a Scheme programmer, just looking this up on the fly. This seems to work for me: (case (string->symbol (substring "CW-F" 0 3)) ('RGB "Found RGB") ('CW- "Found CW-") (else (string-append "Didn't find anything in '" (substring testString 0 3) "'.")) ) More confusion on my part: the apostrophe ' means "stands for itself", up to whitespace? Why doesn't "RGB" work? Doesn't the quote pairs mean stand for itself also? plashless, off banks of noon

Test case 5 works as desired.
Subscribe to Comments for "Case and string?"