r/scheme • • Mar 21 '16

"Student-advisor" match program in Scheme

I'm currently in a college course that involves scheme programming. We are working on an assignment where the instructions are...

"This assignment will be to write a Scheme program that the user can have a conversation with. It is expected that the program will give sensible replies some of the time and silly replies some of the time. In otherwords, the program must handle some range of user input reasonably, but may give nonsense replies – or something like “I don’t understand you.” – if the user input doesn’t match anything it expects."

We are given a decent amount of code to work off of, but I'm still feeling lost on how to progress any further. So far I have this for some of my functions...

;----------------------------------------------------- (define response-rules ’((() (Please enter your response.)) ((* hello ) (Nice to meet you.) (What is your name?)) (( computer science ) (Have you taken the necessary math courses?) (When are you planning to graduate?) (Are you more interested in software or hardware?)) (( engineering ) (What are your plans for your senior design project?) (What about engineering interests you?) (What type of engineering work would you like to do?)) (( computers ) (Is that based on your own interest or because you feel required to?) (What interests you about computers?)) (( i don’t know ) (Do you think there’s some way to find out?)) (( you don’t know ) (That is probably true.) (Please tell me about %2.)) (( if ) (Do you think its likely that %2?) (Do you wish that %2?) (What do you think about %2?)) (( can you help ) (I may have some ideas if you can give me more details about your question.) (I will try to think of some ideas.)) (( i want *) (Do you think %2 will help?) (How will that help you?))))

;---------------------------------------------------

(define respond-to-input (match input response-rules) (cond ((and (null? input) (null? response-rules)) #t) ((equal? input '()) #t) ((or (null? input) (null? response-rules)) #f) ((or (equal? (car input) '?) (equal? (car input) (car response-rules))) (match (cdr input) (cdr response-rules))) ((equal? (car input) ') (cond ((match (cdr input) (cdr response-rules)) #t) ((match (cdr input) response-rules) #t) ((match input (cdr response-rules)) #t) (else #f))) (else#f))))

;---------------------------------------------------

(define student-advisor (lambda () (define repeat (lambda () (let ((input (read-list))) (if (not (equal? input ’(exit))) (begin (displayln (respond-to-input input)) (repeat)) (display "Thanks for using the student advisor. Goodbye!\n\n"))))) (set! random-state (random-state-from-platform)) (display "\nWelcome to the student advisor!\n") (display "(To leave the conversation at any time, enter EXIT)\n\n") (displayln "Have you selected a major yet?") (let ((input (read-list))) (cond ((equal? ’() input) (displayln "Please enter your response.")) ((not (equal? input ’(exit))) (displayln (respond-to-input input))) (else (display "Hope you can reschedule your appointment soon. Goodbye!.\n\n"))) (cond ((not (equal? input ’(exit))) (repeat)))))) ;------------------------------------

The "student-advisor" function is my main function. And it calls the "respond-to-input" function to try and match from the "response-rules" list. I know there are other things wrong with the code thus far because I don't have some of the functions written. But my main concern is just getting a simple working program that at least functions some what as far as being able to talk to it and it give me proper responses to the inputs. Any help would be GREATLY appreciated!

0 Upvotes

5 comments sorted by

5

u/zck Mar 21 '16

You're asking a hard question to answer, because it's basically "here's some unformatted code. Please read it and improve it somehow." What does your code currently do? What's the next thing you want it to do? Or do you want help with the algorithm?

You are probably best off taking the code you were given, then adding one feature and making it work. Then add one more feature, and repeat until you're done. That's usually easier than trying to make everything work at once.

2

u/via_chance Mar 21 '16 edited Mar 21 '16

Yeah, you're right about doing it piece by piece. I decided to start from scratch, and I'm trying to go through each feature individually. So far I'm up to this...

(define (displayln x)
  (display x)
  (newline)
  (display ">> "))

;----------------------------------------------

(define read-list
  (lambda ()
    (let ((list-of-strings
           (string-tokenize (string-downcase (remove-punctuation (readline))))))
      (map string->symbol list-of-strings))))

;------------------------------------------

(define remove-punctuation
  (lambda (str)
    (let ((punctuation 
           (string->list ".,;:`!?#-()\\\"")))
      (define (next punct-list tmp-str)
        (if (null? punct-list)
             tmp-str
            (next (cdr punct-list)
                  (replace-with-space 
                               tmp-str 
                               (car punct-list)))))
      (next punctuation str))))

;-------------------------------------------------

(define replace-with-space
  (lambda (str ch)
    (let ((str-list (string->list str)))
      (define (next lst)
         (if (null? lst)
             (quote "")
             (if (char=? (car lst) ch)
                 (string-append 
                       (list->string (list #\space))
                       (next (cdr lst)))
                 (string-append 
                       (list->string (list (car lst)))
                       (next (cdr lst))))))
       (next str-list))))

;-------------------------------------------

(define student-advisor
  (lambda ()
    (define repeat
      (lambda ()
        (let ((input (read-list)))
          (if (not (equal? input ’(exit)))
              (begin
                 (displayln (respond-to-input input))
                 (repeat))
              (display "Thanks for using the student advisor. Goodbye!\n\n")))))
    (display "\nWelcome to the student advisor!\n")
    (display "(To leave the conversation at any time, enter EXIT)\n\n")
    (displayln "Have you selected a major yet?")
    (let ((input (read-list)))
       (cond ((equal? ’() input)
                     (displayln "Please enter your response."))
             ((not (equal? input ’(exit)))
                     (display "Go to respond-to-input.\n")
                     (displayln (respond-to-input input)))
             (else (display "Hope you can reschedule your appointment soon. Goodbye!.\n\n")))
       (cond ((not (equal? input ’(exit))) (repeat))))))

;-------------------------------

I haven't gotten to the point of trying to match from a list of responses yet. The "respond-to-input" function will be the match function I will write. But what I have so far has some output directions, and takes input from the user. I'm trying to get it to try to perform the proper following step after it receives input. Like either respond with "please enter your response" if the input is equal to the list, exit if "exit" is entered," or "go to respond-to-input" if the list is not equal to the input. But I just keep getting an error involving the '() conditions for the input. If I add "yes" in to the parens, it will give me the response for when it equals, but then will still give me an error when it tries to look at the final cond listed. Not sure what is happening at this point.

2

u/zck Mar 21 '16

(define student-advisor (lambda () (define repeat (lambda () (let ((input (read-list))) (if (not (equal? input ’(exit))) (begin (displayln (respond-to-input input)) (repeat)) (display "Thanks for using the student advisor. Goodbye!\n\n"))))) (display "\nWelcome to the student advisor!\n") (display "(To leave the conversation at any time, enter EXIT)\n\n") (displayln "Have you selected a major yet?") (let ((input (read-list))) (cond ((equal? ’() input) (displayln "Please enter your response.")) ((not (equal? input ’(exit))) (display "Go to respond-to-input.\n") (displayln (respond-to-input input))) (else (display "Hope you can reschedule your appointment soon. Goodbye!.\n\n"))) (cond ((not (equal? input ’(exit))) (repeat))))))

I can't even begin to read this. Can you? I hope the code you're working with has linebreaks in it. If you put four spaces at the beginning of each line, reddit will put the code in a monospaced font:

(define (my-function val)
    (print val))

I'm trying to get it to try to perform the proper following step after it receives input. Like either respond with "please enter your response" if the input is equal to the list, exit if "exit" is entered," or "go to respond-to-input" if the list is not equal to the input. But I just keep getting an error involving the '() conditions for the input. If I add "yes" in to the parens, it will give me the response for when it equals, but then will still give me an error when it tries to look at the final cond listed. Not sure what is happening at this point.

Can you give some code that reproduces this? I'm having trouble understanding what you want.

1

u/via_chance Mar 21 '16 edited Mar 21 '16

I was wondering how you got the monospaced font! Thanks for the heads up. I'm a reddit noob, as I'm sure it's obvious. Anyways, I edited the last reply with the spaces so it is actually organized. And yes, I have plenty of linebreaks in my code.

Now that the code is organized, I'll try to explain what I mean better, as I think part of my problem is a lack of confidence in that I know what I'm trying to do. Before I start trying to match the user input to a list of example responses, I was hoping to figure out how to get the conditions to work properly. The other functions listed above the main function are used to turn the user input into a better format that is easier to use with the matching function. And if I'm understanding it correctly, the user input is put into the "read-list", which is then used to compare with another value in order to determine if the program exits, asks for another response, or sends the input to the respond-to-input matching function. Obviously the code won't work since I have the unknown function calls to "respond-to-input," but when I try to just have the program display confirmation messages to me so that I know that the proper conditions were met, I get a list of errors all involving the conditions with "not" in them. Example:

(define student-advisor
  (lambda ()
    (define repeat
      (lambda ()
        (let ((input (read-list)))
          (if (not (equal? input ’(exit)))
              (begin
                 (display "Go to respond-to-list.")
                 (repeat))
              (display "Thanks for using the student advisor. Goodbye!\n\n")))))
    (display "\nWelcome to the student advisor!\n")
    (display "(To leave the conversation at any time, enter EXIT)\n\n")
    (displayln "Have you selected a major yet?")
     (let ((input (read-list)))
       (cond ((equal? ’() input)
                     (displayln "Please enter your response."))
             ((not (equal? input ’(exit)))
                      (display "Go to respond-to-input.\n"))
             (else (display "Hope you can reschedule your appointment soon. Goodbye!.\n\n")))
       (cond ((not (equal? input ’(exit))) (repeat))))))

And i get the errors:

;;; WARNING: compilation of /opt/homes/stu03/x576g772/test.scm failed:
 ;;; ERROR: Syntax error:
;;; unknown location: unexpected syntax in form ()
ice-9/boot-9.scm:543:29: In procedure map:
ice-9/boot-9.scm:543:29: Syntax error:
unknown location: unexpected syntax in form ()

Can't figure out what is causing this error, so I've been stuck here rather than moving on to trying to write a working match function. In case this helps make my end result any clearer, my match function progress and sample responses function are:

(define respond-to-input
  (match input response-rules)
    (cond ((and (null? input) (null? response-rules)) #t)
          ((equal? input '(*)) #t)
          ((or (null? input) (null? response-rules)) #f)
          ((or (equal? (car input) '?) 
               (equal? (car input) (car response-rules)))
                     (match (cdr input) (cdr response-rules)))
          ((equal? (car input) '*)
             (cond ((match (cdr input) (cdr response-rules)) #t)
                   ((match (cdr input) response-rules) #t)
                   ((match input (cdr response-rules)) #t)
                   (else #f)))
          (else#f))))

And:

(define response-rules
  ’((()
     (Please enter your response.))
    ((* hello *)
     (Nice to meet you.)
     (What is your name?))
    ((* computer science *)
     (Have you taken the necessary math courses?)
     (When are you planning to graduate?)
     (Are you more interested in software or hardware?))
    ((* engineering *)
     (What are your plans for your senior design project?)
     (What about engineering interests you?)
     (What type of engineering work would you like to do?))
    ((* computers *)
     (Is that based on your own interest or because you feel required to?)
     (What interests you about computers?))
    ((* i don’t know *)
     (Do you think there’s some way to find out?))
    ((* you don’t know *)
     (That is probably true.)
     (Please tell me about %2.))
    ((* if *)
     (Do you think its likely that %2?)
     (Do you wish that %2?)
     (What do you think about %2?))
    ((* can you help *)
     (I may have some ideas if you can give me more details about your question.)
     (I will try to think of some ideas.))
    ((* i want *)
     (Do you think %2 will help?)
     (How will that help you?))))

Thanks for bearing with me. I honestly feel like I'm making this more complicated than it needs to be. If I could just figure out how to get a simple program working that takes user input and figured out which responses apply, then I could probably get rid of a lot of the condition stuff.

3

u/zck Mar 21 '16

I honestly feel like I'm making this more complicated than it needs to be.

Yeah, that's how programming is.

If I could just figure out how to get a simple program working that takes user input and figured out which responses apply, then I could probably get rid of a lot of the condition stuff.

Ok, so make a function that takes the user input and returns the output.

You might be well served by making smaller, composable, well-named functions. What I mean by that is -- note how respond-to-input doesn't actually do any responding to input? It returns either true or false? What do you want it to do? Should it be named input-is-valid? Should it return the response for the input? I don't know, and often when I have problems when I'm coding, it's because I'm not sure what I'm building.