Write a function in Lisp (Scheme) called run-cmd that takes three parameters. Th
ID: 3672867 • Letter: W
Question
Write a function in Lisp (Scheme) called run-cmd that takes three parameters. The first is a symbol “command” that defines what to do with the remaining 2 parameters.Your function must support the following commands:
’add -- adds the two parameters using + ’times -- multiplies the two parameters using * ’concat -- string-append the two parameters
Use the cond form. This is the scheme equivalent of a sequence of chained if/else if statements. Here’s an example of cond that looks at a symbol to look up a message.
(define (lookup-msg tag) (cond ((eq? tag ’hello) "Hello world") ((eq? tag ’goodbye) "Goodnight, Moon") ((eq? tag ’scuzz) "You are a scuzz bucket") (else (string-append "Unknown command: " (symbol->string tag)))))
You would call this function like this: (lookup-msg ’hello) Your function will be called like this: (run-cmd ’times 3 8) (this returns 24).
Write a function in Lisp (Scheme) called run-cmd that takes three parameters. The first is a symbol “command” that defines what to do with the remaining 2 parameters.
Your function must support the following commands:
’add -- adds the two parameters using + ’times -- multiplies the two parameters using * ’concat -- string-append the two parameters
Use the cond form. This is the scheme equivalent of a sequence of chained if/else if statements. Here’s an example of cond that looks at a symbol to look up a message.
(define (lookup-msg tag) (cond ((eq? tag ’hello) "Hello world") ((eq? tag ’goodbye) "Goodnight, Moon") ((eq? tag ’scuzz) "You are a scuzz bucket") (else (string-append "Unknown command: " (symbol->string tag)))))
You would call this function like this: (lookup-msg ’hello) Your function will be called like this: (run-cmd ’times 3 8) (this returns 24).
Write a function in Lisp (Scheme) called run-cmd that takes three parameters. The first is a symbol “command” that defines what to do with the remaining 2 parameters.
Your function must support the following commands:
’add -- adds the two parameters using + ’times -- multiplies the two parameters using * ’concat -- string-append the two parameters
Use the cond form. This is the scheme equivalent of a sequence of chained if/else if statements. Here’s an example of cond that looks at a symbol to look up a message.
(define (lookup-msg tag) (cond ((eq? tag ’hello) "Hello world") ((eq? tag ’goodbye) "Goodnight, Moon") ((eq? tag ’scuzz) "You are a scuzz bucket") (else (string-append "Unknown command: " (symbol->string tag)))))
You would call this function like this: (lookup-msg ’hello) Your function will be called like this: (run-cmd ’times 3 8) (this returns 24).
Explanation / Answer
(define (lookup-msg tag)
(cond ((eq? tag 'times num1 num2) (*(num1 num2)))
((eq? tag 'add) num1 num2) (+(num1 num2))
(else (string-append "Append: " (num1->num2)))))
In this program we are checking with operation command, then giving resulting
answer.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.