views:

42

answers:

1

Hi All,

I am not able to understand the error with the code below which simply prints the length of the string:

(define codeLen (read))
(display codeLen)
(define code (read))
(display code)
(string-length code)

I am getting an error : string-length: expects argument of type <string>; given a

regards,
darkie

+2  A: 

You've probably entered a. read reads an arbitrary piece of s-expression, and in this case, it reads the symbol a. If you enter "a" instead, you will get a string.

But more likely you will want to use the read-line function.

Eli Barzilay
Is it (define code (read-line)) ? I read online that it needs an input-port, but not sure what it is for DrScheme ? Can you please help me out here?
darkie15
My objective is to use string-length to check for null string. However, I am not able to achieve this using quotes in the input.
darkie15
You could `(define code (read-line))`, but `code` is a weird name for a string. The input port is an optional argument that defaults to the current input port.
Eli Barzilay
You don't need `string-length` to check for an empty string, you can simply compare it using `equal?` to `""`. You can also get an empty string from `(read)` if you enter `""`.
Eli Barzilay
I changed `code` to `inpStr`. But `read-line` is not recognised by DrScheme
darkie15
You need to be using the default "use the language declared in the source" in the language dialog, and make sure that you have `#lang racket` in the definitions window (or `#lang scheme` if you're using an older version).
Eli Barzilay