PLT Scheme - Need help with debug

Started by
4 comments, last by soulhunter 18 years ago
I have this PLT scheme assignment. that ask us to write a code that turn input into list then count words in the list separated by spaces but when i finish my code there is one bug i can't get rid of it. The bug is when ever there is space in the end the program will crash with cond So i was hoping if someone can help me get rid of it or some pointer that will be very thankful. P.S i think Bolean form the forum is one of my classmate LOL and oh yeah my code look very bad please don't laugh at me Here is my code I used Iteration and recursion:

(define [word-count message]
  (word? 0 0 (string->list message))
  )

(define [word? a b message] 
  
    (cond [(empty? message)(display a)]
          [(and(= b 0)(not(equal? #\space (first message)))) (word? (+ 1 a)(+ 1 b)(rest message))]
          [(and (equal? #\space (first message))(not (equal? #\space (first (rest message)))))(word? (+ 1 a)(+ 1 b)(rest message))]
          [else (word? a (+ 1 b)(rest message))]
        )
  )

Here is input and output

(word-count "hello")  output = 1
(word-count " hello")  output = 1
(word-count "   hello")  output = 1
(word-count "hello ")  output = first: expects argument of type <non-empty list>; given ()
(word-count "Hello  ")  output = first: expects argument of type <non-empty list>; given ()

(word-count "Hello there")  output = 2
(word-count "Hello there ")  output = 2
(word-count "Hello    there ")  output = 2
(word-count "Hello there dood!")  output = 3
(word-count " ")  output = first: expects argument of type <non-empty list>; given ()




</pre>

<!–EDIT–><span class=editedby><!–/EDIT–>[Edited by - soulhunter on April 18, 2006 1:05:34 AM]<!–EDIT–></span><!–/EDIT–>
Advertisement
oh yeah i don't all my forum code worked can't have color or code column LOL,
beside the word counting code we have to that there is also an "ecrption" and "decryption" code we need to write.
The code is base on what ever u input into the program It will add ASCI value by 32.
But i just don't have clue how to change the input into char integer then back to characters.
I hope maybe someone how is good with PLT scheme can give a hand or maybe a direction on how to program it Thank a lot

Here is code they want us to use for ecryption

; Given a printable character, return its
; numeric code
(define [printchar->integer char]
(- (char->integer char) 32))
; Given a numeric code between 0 and 94,
; inclusive, return the corresponding
; printable character
(define [integer->printchar code]
(integer->char (+ code 32)))

Here is what they expect to be the output

• (encipher "Fred" " ") returns "Fred"
because using spaces,which have character code 0, as a key doesn’t shift any characters

• (encipher "abcdefg" "!") returns "bcdefgh"
because ‘!’ has character code 1, so it causes each letter in the message to be moved ‘up’ one place

• (encipher "Beware the Ides of March" "#") returns "Ehzduh#wkh#Lghv#ri#Pdufk"

• (encipher "Beware the Ides of March" "Julie") returns "l[dKX0uaRKJ?QOYJeSi3,hPR"

• (encipher "short text" "very long key") returns "jNbltldT‘t"

• (encipher "See Spot run" "YourKey") returns "-U[rViNohh:"

• (encipher "See Spot run" "MyKey") returns "! 1eM>i@elCh"
showing that encrypting the same message with different keys produces different results

• (encipher "! 1eM>i@elCh" "R&T:&") returns "See Spot run"
which reveals that we can also use our encryption procedure to decrypt if we can find the right key

• (encipher (encipher "Gaius Julius Caesar was here!" "Julie") "U*36:") returns "Gaius Julius Caesar was here!" because "U*36:"
is the decryption key corresponding to encryption key "Julie"

• (encipher "The quick brown fox jumps over the lazy dog!" "qwerty") returns "F‘Krfo[[QrWlaoTr[ijwPhbjewUiZlqlNXtfSr rYiYx"

• (encipher "F‘Krfo[[QrWlaoTr[ijwPhbjewUiZlqlNXtfSr rYiYx" ".(;-+&") returns "Thf quicl browo fox kumps pver tie lazz dog!" which shows the effect of trying to decrypt a message using a slightly imperfect decryption key (one character is wrong in this case)

Your bug is in
[(and (equal? #\space (first message))(not (equal? #\space (first (rest message)))))(word? (+ 1 a)(+ 1 b)(rest message))]

Basically if the current character is a space, if so it then checks the next character. The problem is that it assumes that there is another char. The simplest fix is to check if (rest message) is empty.


Quote:But i just don't have clue how to change the input into char integer then back to characters.


string->list and char->integer can be used turn a string into an integer list (I haven't programmed it, but it should work). And the reverse can be handled by list->string and integer->char.
Original post by Cocalus
Your bug is in
[(and (equal? #\space (first message))(not (equal? #\space (first (rest message)))))(word? (+ 1 a)(+ 1 b)(rest message))]

Basically if the current character is a space, if so it then checks the next character. The problem is that it assumes that there is another char. The simplest fix is to check if (rest message) is empty.



ic but what code should i use or to put to correct the bug?
sorry i am kinda dump with scheme language @_@
it took me so long just to figure the codes
and debug is just almost impossible >_<
so if it is possible can you change the bug for me
i will be very thankful

Again thank for the help
Since this is homework I wont just give you the answer, but I'll give you the pieces needed to create it.

(empty? list) returns true (#t) if list is an empty list and false (#f) otherwise.

(first list) errors if list is empty

(rest list) returns an empty list, if list contains a single element. (Note this also errors if list is empty)

you can check if a list contains a single element by checking if the rest of the list is empty.

(and exp1 exp2) if exp1 is false then exp2 is never evaluated. For example

(/ 1 0) will have a divide by zero error
(= 1 0) will return false
(and (/ 1 0) (= 1 0)) will have a divide by zero
(and (= 1 0) (/ 1 0)) will return false since and doesn't evaluate (/ 1 0)

(Note: (or exp1 exp2) wont evaulate exp2 if exp1 is true)

And can be applied to more than 2 expressions so
(and true true true true true true true false (/ 1 0)) returns false
(and true true true true true true true (/ 1 0) false) has a divide by zero error
Quote:Original post by Cocalus
Since this is homework I wont just give you the answer, but I'll give you the pieces needed to create it.

(empty? list) returns true (#t) if list is an empty list and false (#f) otherwise.

(first list) errors if list is empty

(rest list) returns an empty list, if list contains a single element. (Note this also errors if list is empty)

you can check if a list contains a single element by checking if the rest of the list is empty.

(and exp1 exp2) if exp1 is false then exp2 is never evaluated. For example

(/ 1 0) will have a divide by zero error
(= 1 0) will return false
(and (/ 1 0) (= 1 0)) will have a divide by zero
(and (= 1 0) (/ 1 0)) will return false since and doesn't evaluate (/ 1 0)

(Note: (or exp1 exp2) wont evaulate exp2 if exp1 is true)

And can be applied to more than 2 expressions so
(and true true true true true true true false (/ 1 0)) returns false
(and true true true true true true true (/ 1 0) false) has a divide by zero error


To be honest i got more confused now.
Guess i will just hand the one with bug then
anyway thx for the help but i am just too dumb to understand it >.<

This topic is closed to new replies.

Advertisement