CSE 413 -- Scheme Mini-Exercises
These mini-exercises are to do in class, or afterwards on your own or in groups. They
are not to be handed in or graded. Check your answers on the computer as appropriate.
- Write a Scheme expression to compute 3*x+y+10. (Assume that the variables x and y are
properly bound.)
- What is the result of evaluating the following expressions?
(car '(a b c))
(cdr '(a b c))
(car (cdr '(a b c)))
(number? (cdr '(1 2)))
(cons 1 ())
(list 1 ())
- Suppose we evaluate
(define x 3)
(define y 10)
Then what is the result of evaluating
- Define a function to compute the cube of a number.
- Joe Hacker states loudly that there is no reason or in Scheme needs to be
special -- it can just be defined by the programmer, like this:
(define (or x y)
(if x #t y))
Is Joe right?
- Given the following definitions
(define x 100)
(define y 200)
(define (test x)
(frog x))
(define (frog y)
(+ x y))
what is the result of evaluating
- Define a recursive function that takes a list of numbers and returns a list of their
square roots. Thus (sqrts 1.0 2.0 4.0) should return (1.0 1.41421 2.0).
(Hint: the answer to the question you were about to ask is sqrt.) What recursion
pattern does this function use?
- Define a function that takes a list of numbers (nested to any depth) and returns the sum
of the numbers. In other words, the argument can be a number, a list of lists of lists of
numbers, etc. Thus (sum '(1 (2) ((3 4)) 5)) should return 15. What recursion
pattern does this function use?
- Give a different (non-recursive) definition of the sqrts function using map.