I just started racket so excuse me if this is a dumb question. Can someone tell me what exactly I am doing wrong? I am trying to find the value of:
(cond
[(<= (string-length s) 5) 10]
[(string=? s “hello”) 50]
[(string=? (substring s 1 4) “ell”) 25]
[else 130])
when when s is (i) "hello" and s is (ii) "hellos" .
I did one similar with numbers where all I had to do was define it with a number and had no problems. I don’t know what I am doing wrong here.
edit-
like for this one. Where they ask for the value of n when its 150000.
I simply define n as 150000I get “mb” as the value.
;;(define n 15000)
(cond
[(<= n 1000) (number->string n)]
[(<= n 5000)
(string->symbol
(string-append "num" (number->string n)))]
[else (substring
(string-append "number" (number->string n)) 2 4)])
i am trying to do the ame with the above. if this makes any sense.
Notice that strings must be surrounded by
""to work, in your code you’re using a similar but not-quite-right kind of double quotes. Try this instead:Besides that, the code has no errors per-se, but it’s possible that the result obtained is not the one you expected because of the order in which the different conditions appear in the
cond, try switching some, reorganizing them, etc.Bear in mind that the conditions are evaluated from top-to-bottom and the first condition that is true will be executed. For example, if the input is
"hello",(<= (string-length s) 5)gets evaluated and executed before(string=? s "hello"), because the first condition is true, and the second condition is never reached and therefore never executed.