Samariyu
Samariyu

Reputation: 15

Having trouble casting string to list in racket

I'm pretty new to racket and I'm having trouble with casting a string to a list. I'm working on making a basic boggle game for a class right now, and I keep getting this error from the following code :

car: contract violation expected: pair? given: "aarhus" <--error

(define (word-is-in? a-word a-rand-list)
  ((cond
     [(string? a-word)(string->list a-word)]
     )
   (cond
    [(empty? a-word) #t] ;; Found all letters in a-word
    [(empty? a-rand-list) #f]
    [(is-in (car a-word) a-rand-list)
      (word-is-in? (cdr a-word)
                   (remove-item (car a-word) a-rand-list))
    ]
    [else #f] ;; first letter of a-word not in a-list

  )
 )
)

what this function is supposed to do is read in a word from a list of words (a-word) return #t IFF all the letters in a-word are in a-list. I think my problem is that a-word is being read in as a string. That is why I added in

(cond
   [(string? a-word)(string->list a-word)]
)

at the top of the function. I was hoping it would cast the string to a list, yet I am still getting this error. Thoughts?

Upvotes: 1

Views: 330

Answers (1)

KeV
KeV

Reputation: 2891

The problem lies in here [(is-in (car a-word) a-rand-list) ...]. a-word is still a string, hence you can not call car on it.

I suppose you have 2 separate conditionals to first "cast" the string into a list and in the second conditional use that list?

((cond
     [(string? a-word)(string->list a-word)]
     )

If that is what you want to do you should do (set! a-word (string->list a-word)). This is because string->list will return the string as a list, but won't assign it to a-word.

PS : In this case i would change the first conditional into a when statement.

Upvotes: 2

Related Questions