scam FILENAMEor
scam -r FILENAMEwhere
FILENAME
is replaced by the name of the program you wish to run.
The
-r
option will automatically run a no-argument function named
main
on startup.
(define (main) (println "AUTHOR: Rita Recursion rrita@crimson.ua.edu") )with the name and email replaced by your own name and email.
(for-loop (range 0 10 1) (lambda (i) (inspect i)))should produce the following output:
i is 0 i is 1 i is 2 ... i is 9You will need to implement the range function, which returns a list of numbers generated from the arguments. The first argument specifies the start of the range (inclusive) while the second argument specifies the end (exclusive). The third argument is the step size. The function should run in linear time.
$ cat task1.args (println (range 0 10 5)) (for-loop (range 0 10 5) (lambda (x) (inspect x))) $ scam -r task1.scm task1.args (0 5) x is 0 x is 5 $ scam -r task1.scm task1.args $As a reminder, you will need to evaluate every expression in the input file. To do so, you will need version 2.4c of Scam or higher. Use the standard read pattern:
(define env this) (define (iter expr) (if (not (eof?)) (begin (eval expr env) (iter (readExpr)))) ) (iter (readExpr))
(f a b c) ((curry f a) b c) (g v w x y z) (((curry g v w ) x) y z)Note that curry is variadic and that the syntax of variadic functions in Scam is different than that of Scheme.
(+ 1 2 3 4) (apply + (list 1 2 3 4))You can determine the number of arguments a function expects by asking for the length of its formal parameter list:
(length (get 'parameters f)) ; determine the parameter countYour curry function will only be tested with functions taking a fixed number of arguments.
$ cat task2.args (define (f a b) (+ a b)) (inspect ((curry f) 1)) (inspect (((curry f) 1) 1)) $ scam -r task2.scm task2.args ((curry f) 1) is <function anonymous(@)> (((curry f) 1) 1) is 2 $
; read some ints and place them in both a stack and queue (define (loop stack queue) (define x (readInt)) (if (eof?) (list stack queue) (loop (push stack x) (enqueue queue x)) ) ) ; empty out a stack, printing the values as they come off (define (popper s mode) (cond ((!= (ssize s) 0) (if (= mode 1) (inspect (speek s))) (popper (pop s) mode) ) ) ) ; empty out a queue, printing the values as they come off (define (dequeuer q mode) (cond ((!= (qsize q) 0) (if (= mode 1) (inspect (qpeek q))) (dequeuer (dequeue q) mode) ) ) ) (define (main) (define oldstream (setPort (open (get ScamArgs 1) 'read))) (define mode (get ScamArgs 2)) (define data (loop (Stack) (Queue))) (setPort oldStream) (println "popping...") (popper (car data) mode) (println "dequeuing...") (dequeuer (cadr data) mode) )Example:
$ cat task3.args (println (speek (pop (pop (push (push (push (Stack) 3) 2) 1))))) (println (qpeek (dequeue (dequeue (enqueue (enqueue (enqueue (Queue) 1) 2) 3))))) $ scam -r task3.scm s task3.args 3 3 $
(let*->lambdas `(define (f x) (let* ((y 2) (z y)) (+ x y z))))should return the list:
(define (f x) ((lambda (y) ((lambda (z) (+ x y z)) y)) 2))The let* may have one or more variables in its variable definition list and you may assume the given function, if it contains a let*, has the let* as its body. You do not need to convert let*s recusively.
$ cat task4.args (println (let*->lambdas `(define (f x) (let* ((y 2) (z y)) (+ x y z))))) $ scam -r task4.scm task4.args (define (f x) ((lambda (y) ((lambda (z) (+ x y z)) y)) 2)) $
$ cat task5.args (println (((create 4) (lambda (x) (+ x 1))) 0)) (println (((pred (create 4)) (lambda (x) (+ x 1))) 0)) $ scam -r task5.scm task5.args 4 3 $
(s e t)can be represented as:
(nil (s) (e) (t) (s e) (s t) (e t) (s e t))or:
(nil (t) (e) (e t) (s) (s t) (s e) (s e t))as well as many other ways. Only the first representation shown is in canonical order. Enforcing canonical order at any step should only take linear time.
(powerSet '(a b))Example:
$ cat task6.args (println (powerSet '(a b))) $ scam -r task6.scm task6.args (nil (a) (b) (a b)) $
nil
:
(setNilDisplay 'nil)
((1 4 6) (2 5 7) (3 6 8) (4 6 9))
.
$ cat task7.args (println (transpose '((1 0) (0 1)))) (println (matrix-*-matrix '((1 2) (3 4)) '((1 0) (0 1)))) $ scam -r task7.scm task7.args ((1 0) (0 1)) ((1 2) (3 4)) $
(define (node value left right) (define (display) (print value)) this ) (define (newBST value) (node value nil nil) ) (define (displayBST root) (define (iter root indent) (if (valid? root) (begin (iter (root'right) (string+ indent " ")) (print indent) ((root'display)) (println) (iter (root'left) (string+ indent " ")) ) ) ) (iter root "") )Define an insertBST function that takes a binary search tree and a value and returns a new binary search tree that includes that value. You may assume that only unique values are inserted into the tree.
$ cat task8.args (define t0 (newBST 5)) (define t1 (insertBST t0 2)) (define t2 (insertBST t1 8)) (displayBST t2) $ scam -r task8.scm task8.args 8 5 2 $Note that the BST is displayed sideways.
submit proglan lusth test2For your final submission, use the command:
submit proglan lusth assign2