Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Scheme/Dr.Racket Help

Name: Anonymous 2012-10-23 20:56

Using Scheme/DrRacket for this problem:

I need help /g/...

This problem is telling me to do the following:

Create a Scheme function has-consequence? that consumes word (a string with one or more lower-case letters). It produces true if all the letters in word appear in the word “consequence", and false otherwise.



I understand that I have to use recursion and lists for this. So far all I've been able to do is convert the word that I'm supposed to use as a list by doing the following:

string->list "consequence"



However, how do I make the program convert the argument "word" into a list and then compare "consequence" with "word" to see if all characters in the "word" appear in "consequence"?

Name: Anonymous 2012-10-23 22:34

Alright, so you have string, which you are already able to turn into a list via string->list.

What I would do is:

1. Create a procedure to tell you if a given character is in the word consequence.

2. Within another, see if all characters satisfy said condition.

Assuming you don't know reduce, memq, or really, anything else which would have made this a fucking one-liner.



(define (is-in? char sequence)
  (and (not (null? sequence))
       (or  (equal? char (car sequence))
            (is-in? char (cdr sequence)))))

(define (combination-of? word char-list)
  (define length (string-length word))
  (define (combination-of-helper index result)
    (if (or (not result) (= index length))
        result
        (combination-of-helper (+ index 1)
                               (is-in? (string-ref word index) char-list))))
  (combination-of-helper 0 #t))

(combination-of? "c" (string->list "consequence"))

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List