Problem Solving in Scheme: Evaluation, Writing Procedures, and Recursive Procedures, Exercises of Computer Programming

Problem solutions in scheme programming language. It includes evaluating expressions, writing procedures for days to minutes and minutes to days conversions, computing the hypotenuse of a right triangle, writing xor logical operator, and writing recursive procedures for multiplication and even number check.

Typology: Exercises

2012/2013

Uploaded on 04/25/2013

lathika
lathika 🇮🇳

4

(12)

167 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Problem 1: Evaluation
The usual: type of expression, guess the value, then evaluate.
(lambda (x y z) x)
((lambda (x y) (+ x y)) 4 (+ 3 4))
((lambda (happy tiger) (if (= tiger 4) (+ happy tiger) happy))
4 5)
((lambda (wow this works) (wow this works))
- 7 5)
((lambda (wow this works) (works wow this))
7 - 5)
((if (= 4 5)
(lambda (x) (+ x 3))
(lambda (x) (+ x 5)))
7)
(define x 2)
((lambda (x) (+ x x)) 5)
((lambda (yummy) (* yummy yummy)) 5)
yummy
Problem 2: Writing Procedures
For each problem, write the specified procedure while obeying the given constraints on what primitive
procedures are available. Additionally, test the procedure with a couple of inputs and include these test
cases and their results in your submission to demonstrate that your procedure works.
1. If you didn't finish problem 2 in the lecture handout, do so (through positive-root).
2. Write two procedures that use the following definitions to convert days into minutes and minutes
into days:
Docsity.com
pf3

Partial preview of the text

Download Problem Solving in Scheme: Evaluation, Writing Procedures, and Recursive Procedures and more Exercises Computer Programming in PDF only on Docsity!

Problem 1: Evaluation

The usual: type of expression, guess the value, then evaluate.

(lambda (x y z) x) ((lambda (x y) (+ x y)) 4 (+ 3 4)) ((lambda (happy tiger) (if (= tiger 4) (+ happy tiger) happy)) 4 5) ((lambda (wow this works) (wow this works))

  • 7 5) ((lambda (wow this works) (works wow this)) 7 - 5) ((if (= 4 5) (lambda (x) (+ x 3)) (lambda (x) (+ x 5)))

(define x 2) ((lambda (x) (+ x x)) 5) ((lambda (yummy) (* yummy yummy)) 5) yummy

Problem 2: Writing Procedures

For each problem, write the specified procedure while obeying the given constraints on what primitive procedures are available. Additionally, test the procedure with a couple of inputs and include these test cases and their results in your submission to demonstrate that your procedure works.

  1. If you didn't finish problem 2 in the lecture handout, do so (through positive-root).
  2. Write two procedures that use the following definitions to convert days into minutes and minutes into days:

(define seconds-per-minute 60.0) (define minutes-per-hour 60.0) (define hours-per-day 24.0) (define days-per-year 365.25) (days->minutes 1) ;Value: 1440. (minutes->days 1) ;Value: 0.

  1. Given the lengths of the sides of a right triangle, compute the length of the hypotenous. You may use square and sqrt.

(define pythagoras (lambda (a b)

  1. Write the procedure XOR. XOR is a a logical operator like AND or OR, except that it has the following function: If both of the inputs are true, or both of the inputs are false, it returns false. If the inputs differ, it returns true. The truth table for XOR is as follows: X Y Output #t #t #f #t #f #t #f #t #t #f #f #f You can use boolean=? if you want (look it up in the reference manual).

(define xor (lambda (x y)

  1. Write a procedure censor which takes as input a string, and returns either the string or the string "BEEP", depending on whether the input string contained any offensive material. For this problem, any string which contains the phrase "scheme sucks" will be deemed offensive. Thus:

(censor "I love scheme; it's so cool!") ;Value: "I love scheme; it's so cool!" (censor "Dunno, but I think scheme sucks") ;Value: "BEEP"

You may find the primitive procedure substring? handy.

(define censor (lambda (s)