COMP 320: Programming Languages
Scheme Exercise 1
Experiment with Scheme using a Scheme interpreter such as the one found at try.scheme.org. On that page, the pane on the left contains a REPL (read-evaluate-print loop) interpreter, where you can type expressions such as the ones below and it will evaluate them and print the results.
The pane on the right displays a README.html file with some basic instructions. You can click on the + tab next to the README to create a new file and store expressions or functions there. You don’t need to do this until you get to the function definition examples (myMax, contains, numZeros, and myMax2). After you have typed the functions in, you can rename the file by clicking on the tab; Rename is one of the options. You can then type Control-Enter to save the file and load it into the REPL pane, where you can test it.
This page includes the simple examples in the Introduction to Functional Languages slide, as well as some additional simple examples of Scheme expressions and functions. Feel free to experiment with other Scheme expressions!
Examples from Intro to Functional Languages Video
(* 3 7)
(+ 5 7 2)
(- 5 6)
(- 15 7 2)
(-24 (* 4 3))
(sqrt 49)
(sqrt -2)
Examples from Scheme Demo Video
(+ 2 3)
(* 3 7)
(+ 2 3 5)
'alyce
(+ 'alyce 'brady) ;; ERROR! (Double-check this)
'( 'alyce 'brady 'squirrel ) ;; List of 3 strings
'(1 2 3 4 5)
(car '(1 2 3 4 5))
(car '( 'alyce 'brady 'squirrel ))
(cdr '(1 2 3 4 5))
(car '(1))
(cdr '(1))
(cons 1 '(2 3 4 5))
(cons '(1) '(2 3 4 5))
(1 2 3 4 5) ;; ERROR! 1 is not a function
Examples from Scheme tutorial on try.scheme.org.
(display "Hello world!\n")
(expt 2 8)
(define pi 3.14159)
(define (circum r) (* 2 pi r))
Examples from Functional Programming via Scheme Video
These examples are from the second Scheme demo video (Functional Programming via Scheme). You may want to watch that before experimenting with the function definitions below (myMax, contains, numZeros, and myMax2).
Note:
- The
=function can be used to test the equality of numeric expressions. - The
eq?function tests whether 2 items refer to the same object in memory; its behavior with numbers is implementation-dependent. - The
equal?function tests whether 2 items are structurally equivalent with the same values, even if not the exact same object in memory, and so can be used with either atoms or lists. - The
eqv?function does numeric comparisons on numbers (like=), and identity comparisons (likeeq?) on anything else.
The examples below use = and equal?, which are better choices than the eq? used by the video.
(print "Hello world!")
(* 3 7)
(+ 3 7)
(+ 3 7 1 21 4)
(sqrt 16)
;; some relational operators
(< 3 15)
(= 8 15)
(equal? 8 15)
(= 8 (* 2 4))
(= 8 '(8))
(equal? 8 '(8))
(max 3 7)
(max 3 7 1 21 4)
;; if/then expressions: (if <condition> <then-expr> <else-expr>)
(if (> 3 7) 3 7)
;; You will probably want to put the functions below in a file using
;; the editor in the right-hand pane, then test them from the REPL pane.
(define (myMax a b) (if (> a b) a b))
(myMax 8 15)
(myMax 82 15)
;; myMax only looks at 1st 2 operands
(myMax 82 15 63 4 93)
;; (There is also the built-in max function you've used above.)
;; simple recursion example showing define, null?, eq?, #t, #f
(define (contains aVal aList)
(if (null? aList) #f ;; base case
(if (eq? aVal (car aList)) ;; is 1st element it?
#t ;; then true
(contains aVal (cdr aList))) ;; else check rest of list recursively
))
;; example of cond (multi-way conditional), null?, list?, zero?
(define (numZeros ls)
(cond
((null? ls) 0) ;; base case
((list? (car ls)) ;; is 1st elt a list?
(+ (numZeros (car ls)) (numZeros (cdr ls))))
((zero? (car ls)) ;; is 1st elt a 0?
(+ 1 (numZeros (cdr ls))))
(else (numZeros (cdr ls))) ;; how many in rest of list?
))
;; new version of myMax that looks through list, not just 1st 2
(define (myMax2 ls)
(cond
((null? ls) '()) ;; base case, return empty list
((null? (cdr ls)) (car ls)) ;; only 1 elt in list, return it
((> (car ls) (myMax2 (cdr ls))) (car ls)) ;; 1st elt is max
(else (myMax2 (cdr ls))) ;; return max from rest of list
))