We are ready to represent the best custom paper writing assistance that can cope with any task like jusk junk even at the eleventh hour. The matter is that we posses the greatest base of expert writers. Our staff of freelance writers includes approximately 300 experienced writers are at your disposal all year round. They are striving to provide the best ever services to the most desperate students that have already lost the hope for academic success. We offer the range of the most widely required, however, not recommended for college use papers. It is advisable to use our examples like jusk junk in learning at public-education level. Get prepared and be smart with our best essay samples cheap and fast! Get in touch and we will write excellent custom coursework or essay especially for you.
this is a piece of junk, just disregard it....
This is the html version of the file http//bmrc.berkeley.edu/~beckmann/files/Midterm1.rev.sol.pdf.
G o o g l e automatically generates html versions of documents as we crawl the web.
To link to or bookmark this page, use the following url http//www.google.com/search?q=cacheABO4qJLKLxAJbmrc.berkeley.edu/~beckmann/files/Midterm1.rev.sol.pdf+%scheme%+compose+list&hl=en&ie=UTF-8
Do my coursework
Google is not affiliated with the authors of this page nor responsible for its content.
These search terms have been highlighted scheme compose list
--------------------------------------------------------------------------------
Page 1
CS 61A Midterm 1 Review Solutions 1. Here are some functions (define double (lambda (x) ( x ))) (define olympics (lambda (x) (lambda (g) (g (g x))))) And here is a Scheme expression. All it’s missing are parentheses; insert parentheses where needed to make the expression return a number. ( (olympics ( (olympics )double) )double)The expression ((olympics ) double)returns 8, which turns the above into ((olympics 8) double), which returns . . Figure out what Scheme will return or do when each of the following expressions is evaluated. STk (()) Error � is not a procedureSTk ((let () (lambda () ))) STk (define a (list ‘how (cons ‘are ‘you))) STk (define b (append a ‘(a) ‘(a))) STk (cdr (list-ref (cons a b) )) Error � can’t take the cdr of the symbol a.STk (every cadr ‘(one two three four)) Error � you cannot take the cadr of a word; cadr only works on pairs. STk ((car (list ((lambda () car)))) (who let the dogs out)) who STk ((first ‘first) ‘butfirst) Error � the word f is not a procedureSTk (cond (first hello) (car (1 4)) (else 1)) hello-- Converted from Word to PDF for free by Fast PDF -- www.fastpdf.com --
--------------------------------------------------------------------------------
Page
. Write a function running-total, that takes a non-empty sentence of numbers and returns another sentence of the running totals. In other words, the first number in the resulting sentence should be the first number of the argument sentence; the second number in the resulting sentence should be the sum of the first and second numbers in the argument sentence, and so on. STk (running-total ‘(1 4)) (1 6 10) STk (running-total ‘(-5 0 � 18 55)) (-5 �5 �7 � 46) STk (running-total ‘()) () See if you can write this function without defining any helpers! ;; The no-helper version (define (running-total sent) (if (empty? sent) ‘() (se (first sent) (every (lambda (n) (+ n (first sent))) (running-total (bf sent)))))) ;; A common solution that uses a helper (define (running-total sent) (define (little-help nums total) (if (empty? nums) ‘() (se (+ (first nums) total) (little-help (bf sent) (+ total (first nums)))))) (little-help sent 0)) 4. In homework we wrote a function called compose, that returned the composition its two argument functions (define (compose f g) (lambda (x) (f (g x)))) We want you to take this function to the next level by writing compose-listthat takes a list of one-argument functions, and returns a function representing their composition. The functions should be composed such the first function in the list is the outermost function in the composition, i.e. given a list of functions each with one argument x, (list a b c)is composed as (a (b (c x))). For example STk (define (a x) (+ x 1)) STk (define (b x) ( x )) STk (define (c x) (- x 4))STk (define d (compose-list (list a b c)))STk (d 1) -8 -- Converted from Word to PDF for free by Fast PDF -- www.fastpdf.com --
--------------------------------------------------------------------------------
Page
You can use helper functions if necessary. (Hint use compose.) Use only cons, car, and cdrto manipulate lists (do not use sentence, first, butfirst, etc.). Also, do not use any higher order functions. (define (compose-list procs) (if (null? procs) (lambda (x) x) ;; return identity function if done (compose (car procs) (compose-list (cdr procs))))) ;; Another solution (define (compose-list list) (define (help-compose list x) (if (empty? list) x ((car list) (help-compose (cdr list) x)))) (lambda (x) (help-compose list x))) 5. In the evenings Greg likes to relax by doing environment diagrams, but one night he felt he wanted something different. He decided to write the list?predicate instead. It’s a function that takes a single argument which can be any Scheme type and returns #tif it is a list, #fotherwise. Unfortunately, Greg is only good at environment diagrams, not predicates, so his first attempt (below) is flawed. Find the flaw and fix it. (Hint what will (list? ‘( . ))return?). (define (list? thing) (or (null? thing) (list? (cdr thing))));; We need to make sure that “thing” is a pair before trying to take its cdr. (define (list? thing) (or (null? thing) (and (pair? thing) ;; new (list? (cdr thing))))) 6. Define a function interleavethat takes two words as arguments and returns a new word with their letters interleaved. The words need not be of the same length. You may use exactly one ifin your solution and no other conditionals. Here are some examples STk (interleave hello there) htehlelroe STk (interleave cs61ais cool) ccso6o1lais STk (interleave “” foo) ;; “” is the empty word foo STk (interleave bar “”) bar-- Converted from Word to PDF for free by Fast PDF -- www.fastpdf.com --
--------------------------------------------------------------------------------
Page 4
(define (interleave wd1 wd) (if (empty? wd1) wd (word (first wd1) (interleave wd (bf wd1))))) What would happen if you passed two sentences as arguments to interleave? STk (interleave (hello) (two)) Error � the word function does not take the empty sentence as argument. 7. There is a very powerful higher-order function for lists called accumulate. The code is on Page 116 (and reproduced below). It is a Scheme primitive. This function is completely different from the one you wrote in Homework . Those who have taken CS will recognize this is very similar to reduce. (define (accumulate combiner null-val lst) (if (null? lst) null-val (combiner (car lst) (accumulate combiner null-val (cdr lst)))))What do each of the following return? STk (accumulate � 0 ‘(1 4)) -STk (accumulate word “” ‘(who let the dogs out)) wholetthedogsout STk (accumulate (lambda (a b) b) ‘booyah ‘(1 )) booyah STk (accumulate -1000 ‘(1 4)) Error � the function will be called on a number and a boolean. Notice that throughout the computation, the second argument of the function given to accumulate stores the value accumulated so far, beginning with the null value. You can use this fact to do some incredible things with accumulate! Write the function length, which computes the length of a list, using accumulate (define (length lst) (accumulate (lambda (x so-far) (+ so-far 1)) ;; combiner 0 ;; null value lst))Write the function mapusing accumulate (define (map func lst) (accumulate (lambda (x result) (cons (func x) result)) ;; combiner ‘() ;; null value lst)) -- Converted from Word to PDF for free by Fast PDF -- www.fastpdf.com --
--------------------------------------------------------------------------------
Page 5
Now write the function compose-list (from Number 4 on this handout) using � yep, you guessed it! � accumulate (define (compose-list lst) (accumulate compose;; combiner (lambda (x) x);; null value lst))8. Write the function group-that takes one argument, a list whose length is a multiple of three. The function should return a new list with every three consecutive elements grouped into a sublist. Additionally, the last element in the resulting list should be the number of these triples (groups) that were made. For example STk (group- (who let the dogs out early)) ((who let the) (dogs out early) ) STk (group- (1 4 5 6 7 8 )) ((1 ) (4 5 6) (7 8 ) ) (define (group- lst) (define (grouper things count) (if (null? things) (list count) (cons (list (car things) (cadr things) (caddr things)) (grouper (cdddr things) (+ count 1))))) (grouper lst 0)). Write the function for-and-rev?that takes a word and returns true if the word contains a two-letter sequence and the same sequence in the reverse order. STk (for-and-rev? radar) ;; ra & ar #t STk (for-and-rev? pop) ;; po & op #t STk (for-and-rev? ‘aeiou) #f (define (for-and-rev? wd) (if ( (count wd) ) #f (or (substring? (word (first (bf wd)) (first wd)) (bf wd)) (for-and-rev? (bf wd))))) (define (substring? two wd) (if ( (count wd) ) #f (or (equal? two (word (first wd) (first (bf wd)))) (substring? two (bf wd))))) -- Converted from Word to PDF for free by Fast PDF -- www.fastpdf.com --
--------------------------------------------------------------------------------
Page 6
10. What is the order of growth of each of the following functions (dedicated to the Cal football team)? You may assume that foois a function that runs in linear time. In other words, foois ¡£¢¥¤§¦(define (tedford n) (if ( n 100000) 4 (tedford (- n 1)))) Each call to tedford gives rise to at most 1 recursive call, and all other operations (subtraction, comparison and conditional) are ones that run in constant time. The base case is designed to trick you it is irrelevant since when computing orders of growth we are interested in very large n (the worst possible case). (define (boller n) (if ( n 1000) 4 (+ (foo (- n 1)) (foo (- n )) (foo (- n ))))) Boller is not recursive, but it does call a linear function three times n + n + n = n. Constant factors are tossed, yielding n. (define (pass n) (if ( n 10000) 4 (+ (pass (- n 1)) (pass (- n )) (pass (- n ))))) Very much like the way we would compute fib recursively. Pass essentially calls itself three times for each (large) n, thus tripling the work that remains to be done. If you picture the tree that pass generates, every node in the tree has three branches, one for each recursive call. This means that the base of our exponential order of growth will be . (define (ascii n) (if ( n 1000000) 4 (+ (foo (- n 1)) (foo (- n )) (ascii (- n ))))) Ignoring the two calls to foo, ascii is itself linear since it calls itself recursively once for each n. So, the order of growth is at least linear. But two other linear operations are performed for every n n x n = n. ¨�¥ �¥ n))-- Converted from Word to PDF for free by Fast PDF -- www.fastpdf.com --
--------------------------------------------------------------------------------
Page 7
11. Write a function max-sentthat takes one argument, a non-empty sentence of numbers and returns the largest number in that sentence. (Hint you’ll probably need a helper.) Examples STk (max-sent ‘(1 �4)) STk (max-sent ‘(4)) 4 (define (max-sent sent) (define (find-max nums largest) (if (empty? nums) largest (find-max (bf nums) (max largest (first nums))))) ;; max is a Scheme primitive (find-max (bf sent) (first sent)));; Another neat solution (define (max-sent sent) (if (empty? (bf sent)) (first sent) (max (first sent) (max-sent (bf sent))))) 1. Draw the box and pointer diagrams for each of the following (list ‘one ‘two (list 4 5)) (cons ‘foo (list ())) (cons 1 (cons (cons car))) one two 5 4 foo 1 #[proc car] -- Converted from Word to PDF for free by Fast PDF -- www.fastpdf.com --
--------------------------------------------------------------------------------
Page 8
‘(1 . 17) ‘(lambda (x) ( x x)) (list ‘(lambda (x) ( x x))) ;; Greg is sick of drawing B & P diagrams!(list (cons 1 )) How would the following Box and Pointer diagram be printed? ` (who (() . are) you?) 17 1 lambda x x x who are you? 1 -- Converted from Word to PDF for free by Fast PDF -- www.fastpdf.com --
Mind that the sample papers like jusk junk presented are to be used for review only. In order to warn you and eliminate any plagiarism writing intentions, it is highly recommended not to use the essays in class. In cases you experience difficulties with essay writing in class and for in class use, order original papers with our expert writers. Cheap custom papers can be written from scratch for each customer that entrusts his or her academic success to our writing team. Order your unique assignment from the best custom writing services cheap and fast!
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.