;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Part 1 -- The Evaluator ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Problem 1 print sum 2 3 ; expect 5 print sum product 3 4 8 ; expect 20 print sentence word "now "here last [the invisible man] ; expect nowhere man ;; Problem A2 print "a print "b ; expect a ; expect b ;; Problem A3 print equal? (word "re "deliver) (word "rede "liver) ; expect True ;; Problem B2 print "hello ; expect hello ;; Problem B3 print [this [is a [[deep] [nested]] sentence]] ; expect this [is a [[deep] [nested]] sentence] ;; Problem 4 make "x 12 print sum 5 :x ; expect 17 make "nestednumbers [[10 20 30] 5] print sum (first first :nestednumbers) (last :nestednumbers) ; expect 15 ;; Problem 5 if equal? 1 difference 6 5 [print word "snake "yes] ; expect snakeyes if True [print 3] ; expect 3 if equalp 3 sum 1 2 [print sum 20 10] ; expect 30 ifelse lessp 2 1 [print "Yes] [print "No] ; expect No if 1 [print 3] ; expect First argument to "if" is not True or False: 1 print if True 5 ; expect 5 print if False 5 ; expect None ifelse 1 2 3 ; expect First argument to "ifelse" is not True or False: 1 print ifelse True [sum 1 2] 4 ; expect 3 ;; Extra credit 1 print 3 + 2 ; expect 5 print 2 + 3 + 4 + 5 ; expect 14 print 2 + 3 - 4 + 5 ; expect 6 print 3 * 100 + 2 ; expect 302 print 5 + ifelse 2=3 [6-4] [8/5] ; expect 6.6 ;; Extra credit 2 print 3 + 4 * 5 + 6 ; expect 29 print 3 * 4 + 5 * 6 ; expect 42 print 3 + 12 / 8 - 0.25 * 2 = 2 * ( 1 + 0.5 ) * 4 / 3 ; expect True ;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Part 2 -- Procedures ;; ;;;;;;;;;;;;;;;;;;;;;;;;;; to garply print "hello print "goodbye end garply ; expect hello ; expect goodbye to factorial :n output ifelse equal? :n 1 [1] [product :n factorial difference :n 1] end print factorial 5 ; expect 120 print factorial 4 ; expect 24 to fibonacci :n if equal? :n 1 [output 0] if equal? :n 2 [output 1] output sum fibonacci difference :n 2 fibonacci difference :n 1 end print fibonacci 7 ; expect 8 to err :is :human print :is :human end err "forgive "divine ; expect forgive ; expect You do not say what to do with divine to clobber :n if less? :n 3 [make "clob :n stop] make "clob difference 5 :n clobber difference :n 1 end clobber 5 print :clob ; expect 2 to big :k output greater? :k 5 end print big 7 print big 3 ; expect True ; expect False ;; Scope tests make "x 3 to scope :x helper 5 end to helper :y print (sentence :x :y) end scope 4 ; expect 4 5 to print_last_x print :x end to print_x :x print_last_x end print_x 5 ; expect 5 to one :x output :x end to two :x output one sum :x 1 end print two 1 ; expect 2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Part 3 -- Logo Implementations ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; len outputs the length of sentence :s to len :s output ifelse empty? :s [0] [sum 1 len butfirst :s] end show len [1 2 3 4] ; expect 4 ;; quote adds quotation to a word to quote :w output ifelse word? :w [word "" :w] [:w] end ;; apply_1 applies the named procedure :f to a single argument value :arg to apply_1 :f :arg output run list :f quote :arg end show apply_1 "len [1 2 3 4] ; expect 4 ;; apply_2 applies the named procedure :f to two arguments :arg0 and :arg1 to apply_2 :f :arg0 :arg1 output run fput :f list quote :arg0 quote :arg1 end show apply_2 "word "now "here ; expect nowhere ;; filter returns a subset of :s containing elements for which :f outputs True to filter :f :s ; *** YOUR CODE HERE *** end show filter "big [1 10 3 8 4 7] ; expect [10 8 7] ;; count_change outputs the number of ways to change :total with :denoms ;; At most :max_coins total coins can be used. to count_change :total :denoms :max_coins ; *** YOUR CODE HERE *** end make "us_coins [50 25 10 5 1] print count_change 20 :us_coins 18 ; expect 8 ;; reduce applies two-argument :f cumulatively for items in :s ;; The starting value :n is the first argument to :f to reduce :f :s :n ; *** YOUR CODE HERE *** end print reduce "sum [1 2 3 4 5] 0 ; expect 15 ;; count_partitions outputs the number of ways to partition :total ;; Each partition must be at most :max_value to count_partitions :total :max_value ; *** YOUR CODE HERE *** end print count_partitions 5 3 ; expect 5 ; Note: The 5 partitions are [[3 2] [3 1 1] [2 2 1] [2 1 1 1] [1 1 1 1 1]] ;; list_partitions lists all partitions of :total using at most :max_pieces to list_partitions :total :max_pieces :max_value ; *** YOUR CODE HERE *** end show list_partitions 5 2 4 ; expect [[4 1] [3 2]] show list_partitions 7 3 5 ; expect [[5 2] [5 1 1] [4 3] [4 2 1] [3 3 1] [3 2 2]]