You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Write expressions using only car, cdr, and thing whose values are the list structures given below.
(1)
1
(2 3)
(3)
Problem Lec.5.3.3: Functions on Lists
Part 1: Making a list
What expressions would you have to evaluate in order for x, y, z, and w to have the following values?
x => (())
y => (123)
z => (12 (3))
w => (1 (23) ((4)))
Use cons, nil, and integers. Please don't use list.
Note that there is a right paren over to the right of the input field.
(define x __________ )
(define y __________ )
(define z __________ )
(define w __________ )
Part 2: Some procedures on lists?
Given the values of x, y, z, and w defined above, what are the values of the following expressions?
(We know you could just type these expressions into Scheme and get the values. But please think them through! You won't have a Scheme interpreter available during quizzes!)
Here are the values again:
x => (())
y => (123)
z => (12 (3))
w => (1 (23) ((4)))
(length x)
(length y)
(length w)
(list-ref w 2)
(append x y)
(cons x y)
(append y w)
(cons y w)
(append y y)
(length (append y y))
(cons y y)
(length (cons y y))
(append z z)
(length (append z z))
Lec.5.4.1: Putting out a contract on rationals
For each of the following options (which you should treat as independent), mark the box if the procedures +rat, *rat and other procedures on rationals would still work correctly, without any further changes to the code shown in the lecture.
(define (make-rat n d) (cons d n))
(definenumer cdr)
(definedenom car)
(define (make-rat n d)
(lambda (x) (if (= x 0) n d)))
(define (numer rat) (rat 0))
(define (denom rat) (rat 1))
PS.3.1.1: Is it in my list?
Write a procedure (contains? lst elt) that returns #t if elt is contained (at the top level) in lst, and #f otherwise. You should use equal? to compare items. Here are some examples:
Write a procedure (remove-duplicates lst) that returns a list containing the elements of lst, but with duplicates removed. You may assume that the input is a list of integers. For example:
Write a procedure (union lst1 lst2) that takes two lists of integers (containing no duplicates) and returns a list containing one instance of every integer that is in either list. Here are some examples:
Don't assume anything about the order of the input lists, and don't worry about the order of the elements your output list matches the order in the examples. You may, but are not required to, use contains? and/or remove-duplicates.
(defineunion
(lambda (lst1 lst2) your_code_here))
PS.3.1.4: Subset?
Write a procedure (subset? lst1 lst2) that returns #t if the elements of lst1 are a subset of the elements of lst2. You may assume that the elements are all integers, and that no element appears more than once in a set. Here are some examples:
The elements are arranged in the lists in no particular order. Note that if you have already defined contains? in the previous problem, you may use it here.
Write a procedure (intersection lst1 lst2) that takes two lists of integers (containing no duplicates) and returns a list containing one instance of every integer that is in both lists. Here are some examples:
Don't assume anything about the order of the input lists, and don't worry about the order of the elements your output list matches the order in the examples.
Write a procedure (every-other lst) that takes a list as argument and returns a new list, containing the 0th, 2nd, 4th, etc. elements of the initial list. These elements should appear in the same order that they did in the original list. Here are some examples:
Indicate the type of each of the following expressions. Use the symbols "->" to denote "maps to", for example, the procedure square has type "number->number". Use the following terms to describe primitive types of data: number, boolean, string.
6
(lambda (x) (+ x 1))
"6"
(> 3 4)
((lambda (x) (* x 2)) 4)
(lambda (a b) (+ a (if b 1 0)))
Lec.6.2.1: Types of Procedures
Indicate the type of each of the following procedures.Use the symbols "->" to denote "maps to", for example, the procedure square has type "number->number". Use the following terms to describe primitive types of data: number, boolean, string.
(define (test bar n) (if (bar n) n (test bar (+ n 2))))
(define (test foo bar n) (if (bar n) #t (test foo bar (+ n (foo n)))))
(define (test foo bar n) (if (bar n) (+ 1 (foo n)) (test foo bar (+ n 3))))
Lec.6.3.1: Higher-Order Types
Indicate the type of each of the following expressions. (If the expression defines a procedure, then indicate the type of the procedure so defined.)
(lambda (x) (lambda (y) (+ x y)))
(lambda (p) (+ 1 (p 3)))
(lambda (x y comp) (if (comp x y) (+ x 1) (+ y 1)))
Lec.6.4.1: Accumulating maps and filters
Part 1: Strings to numbers
Suppose you are given a list of strings. Complete the following definition so that applying this procedure to such a list would result in a list of the lengths of each of the strings in the input. Note that string-length will measure the length of a string. (In general, remember that you can look up Scheme procedures in the online reference manual.) For example:
Use map, filter, or fold-right (with the definitions given in lecture) to do this. Please don't write a procedure that involves a recursive application of itself to walk down the input list.
(definelengths (lambda (lst) your_code_here))
Part 2: Strings to one number
Suppose you are given a list of strings. Complete the following definition so that applying this procedure to such a list would result in a sum of the lengths of each of the strings in the input. For example:
Use map, filter, or fold-right (with the definitions given in lecture) to do this. Please don't write a procedure that involves a recursive application of itself to walk down the input list.
Now suppose that you are given a list consisting of either strings or numbers. Complete the following definition so that applying this procedure to such a list would result in the total length of the strings in the list, but not the numbers. (Note that predicates for checkings types, such as number? or string? may be helpful.) For example:
(string-em-up (list"I""really""like"6.001)) => 11
Use map, filter, or fold-right (with the definitions given in lecture) to do this. Please don't write a procedure that involves a recursive application of itself to walk down the input list.
Write a procedure (make-multiplier x), of type number->(number->number), that returns a procedure that takes an argument and multiplies that argument by x. So
For each of the following expressions, if it is valid, indicate its value (if the value is a procedure, simply indicate "procedure"; if the value is unspecified, then indicate "unspecified"); if it is not valid, indicate "error". You may abbreviate "error", "procedure" and "unspecified" by their first letters, "e", "p" and "u" respectively.
Indicate the type of each of the following expressions. If you need type variables, use A,B,C, etc., starting with A as the leftmost variable.
(lambda (x y) x)
(lambda (p) (p 3))
(lambda (p x) (p x))
(lambda (x y comp) (if (comp x y) x y))
(for simplicity, assume x and y are of the same type)
PS.3.2.2: Recursive count-true
Write a recursive (not iterative) procedure, (count-true pred lower upper), of type (number->boolean),number,number->number, that returns the number of integers in the range lower..upper (inclusive) for which predicate pred applied to that number is true.
Write an iterative procedure, (count-true pred lower upper), of type (number->boolean),number,number->number, that returns the number of integers in the range lower..upper (inclusive) for which predicate pred applied to that number is true. Note that you can type multiple defines into the code box below.
(definef (lambda (m b)
(lambda (x) (+ (* m x) b))))
(defineg (f 32))
For each of the following expressions, indicate the result of evaluating it. Possible answers are: a numeric value, the word "procedure" followed by a space and an integer indicating the number of arguments the procedure takes, or "error".
We're matching your answer directly against the answer string, so please don't include extra spaces.
f
g
(* (f 3 2) 7)
(g 6)
(f 6)
((f 4 7) 5)
PS.3.2.6: Repeatedly Apply
Write a procedure (repeatedly-apply p n), of type (A->A),number->(A->A), that returns a procedure that takes a single argument and applies p to it n times. So, for example,
((repeatedly-apply inc 10) 2) => 12
You may assume the following function has been defined (you don't have to use it, but it can contribute to a pretty solution.)
Currying (named after the logician Haskell Curry) is the operation of taking a procedure of multiple arguments and turning it into a higher-order procedure. So a basic version of plus is defined
(defineplus (lambda (a b) (+ a b)))
and used (plus 2 3). A curried version of plus has type number->(number->number), and is defined
(defineplus-c (lambda (a)
(lambda (b)
(+ a b)))) ,
and used
((plus-c 2) 3).
Write your own higher-order procedure, (curry p), of type (A,B->C)->(A->(B->C)), that takes a binary procedure and curries it. So, for example,
(((curry plus) 2) 3) => 5.
(definecurry (lambda (p) your_code_here))
Part 3.3: Optional Problems
PS.3.3.1: Who you callin' odd?
Write a procedure (mix-it-up lst) that takes a list as input, and returns a new list, with the property that for each element of argument list that is odd, it doubles the element, and for each element that is even, it halves the element. You may assume that odd? exists as a predicate. Here are some examples:
Write a procedure, (accumulate-filtered-interval filter op init lower upper), of type (number->boolean),(number,number->number),number,number,number->number that accumulates values as in accumulate-interval, but only those for which the filter predicate returns #t.
(defineaccumulate-filtered-interval
(lambda (filter op init lower upper)
your_code_here))
PS.3.3.3: Counting Truth, Again
Write a new definition of count-true that just makes a single call to accumulate-filtered-interval.
Part 3.1: Cons and Lists
Lec.5.2.1: Practice with Cons
Indicate the value of each of the following expressions. If evaluating the expression will cause an error, type "e".
(car (cons (+ 1 2) (- 3 4)))
(cdr 6)
(cdr (car (cons (cons 1 2) (cons 3 4))))
(cdr (cdr (cons (cons 1 2) (cons 3 4))))
(pair? #t)
(pair? (car (cons 1 2)))
(pair? (cons (+ 1 2) (car (cons 3 4))))
Lec.5.3.1: Practice with Lists
Indicate the value of each of the following expressions. Assume that we have already evaluated the form
If evaluating the expression will cause an error, type "e".
(cons 1 nil)
(cons 1 (cons 2 nil))
thing
(car thing)
(cdr thing)
(car (car thing))
(car (car (car thing)))
(car (cdr (cdr thing)))
Problem Lec.5.3.2: More Practice with Lists
Due date: 2/18, 9am
Assume that we have already evaluated the form
Write expressions using only car, cdr, and thing whose values are the list structures given below.
(1)
1
(2 3)
(3)
Problem Lec.5.3.3: Functions on Lists
Part 1: Making a list
What expressions would you have to evaluate in order for x, y, z, and w to have the following values?
Use cons, nil, and integers. Please don't use list.
Note that there is a right paren over to the right of the input field.
(define x __________ )
(define y __________ )
(define z __________ )
(define w __________ )
Part 2: Some procedures on lists?
Given the values of x, y, z, and w defined above, what are the values of the following expressions?
(We know you could just type these expressions into Scheme and get the values. But please think them through! You won't have a Scheme interpreter available during quizzes!)
Here are the values again:
(length x)
(length y)
(length w)
(list-ref w 2)
(append x y)
(cons x y)
(append y w)
(cons y w)
(append y y)
(length (append y y))
(cons y y)
(length (cons y y))
(append z z)
(length (append z z))
Lec.5.4.1: Putting out a contract on rationals
For each of the following options (which you should treat as independent), mark the box if the procedures +rat, *rat and other procedures on rationals would still work correctly, without any further changes to the code shown in the lecture.
PS.3.1.1: Is it in my list?
Write a procedure (contains? lst elt) that returns #t if elt is contained (at the top level) in lst, and #f otherwise. You should use equal? to compare items. Here are some examples:
PS.3.1.2: Too much of a good thing
Write a procedure (remove-duplicates lst) that returns a list containing the elements of lst, but with duplicates removed. You may assume that the input is a list of integers. For example:
Keep the last of the duplicated elements.
PS.3.1.3: United we stand
Write a procedure (union lst1 lst2) that takes two lists of integers (containing no duplicates) and returns a list containing one instance of every integer that is in either list. Here are some examples:
Don't assume anything about the order of the input lists, and don't worry about the order of the elements your output list matches the order in the examples. You may, but are not required to, use contains? and/or remove-duplicates.
PS.3.1.4: Subset?
Write a procedure (subset? lst1 lst2) that returns #t if the elements of lst1 are a subset of the elements of lst2. You may assume that the elements are all integers, and that no element appears more than once in a set. Here are some examples:
The elements are arranged in the lists in no particular order. Note that if you have already defined contains? in the previous problem, you may use it here.
PS.3.1.5: What do we have in common?
Write a procedure (intersection lst1 lst2) that takes two lists of integers (containing no duplicates) and returns a list containing one instance of every integer that is in both lists. Here are some examples:
Don't assume anything about the order of the input lists, and don't worry about the order of the elements your output list matches the order in the examples.
PS.3.1.6: Every other one
Write a procedure (every-other lst) that takes a list as argument and returns a new list, containing the 0th, 2nd, 4th, etc. elements of the initial list. These elements should appear in the same order that they did in the original list. Here are some examples:
Part 3.2: Higher Order Procedures
Lec.6.1.1: Types of Expressions
Indicate the type of each of the following expressions. Use the symbols "->" to denote "maps to", for example, the procedure square has type "number->number". Use the following terms to describe primitive types of data: number, boolean, string.
6
(lambda (x) (+ x 1))
"6"
(> 3 4)
((lambda (x) (* x 2)) 4)
(lambda (a b) (+ a (if b 1 0)))
Lec.6.2.1: Types of Procedures
Indicate the type of each of the following procedures.Use the symbols "->" to denote "maps to", for example, the procedure square has type "number->number". Use the following terms to describe primitive types of data: number, boolean, string.
(define (test bar n) (if (bar n) n (test bar (+ n 2))))
(define (test foo bar n) (if (bar n) #t (test foo bar (+ n (foo n)))))
(define (test foo bar n) (if (bar n) (+ 1 (foo n)) (test foo bar (+ n 3))))
Lec.6.3.1: Higher-Order Types
Indicate the type of each of the following expressions. (If the expression defines a procedure, then indicate the type of the procedure so defined.)
(lambda (x) (lambda (y) (+ x y)))
(lambda (p) (+ 1 (p 3)))
(lambda (x y comp) (if (comp x y) (+ x 1) (+ y 1)))
Lec.6.4.1: Accumulating maps and filters
Part 1: Strings to numbers
Suppose you are given a list of strings. Complete the following definition so that applying this procedure to such a list would result in a list of the lengths of each of the strings in the input. Note that string-length will measure the length of a string. (In general, remember that you can look up Scheme procedures in the online reference manual.) For example:
Use map, filter, or fold-right (with the definitions given in lecture) to do this. Please don't write a procedure that involves a recursive application of itself to walk down the input list.
Part 2: Strings to one number
Suppose you are given a list of strings. Complete the following definition so that applying this procedure to such a list would result in a sum of the lengths of each of the strings in the input. For example:
Use map, filter, or fold-right (with the definitions given in lecture) to do this. Please don't write a procedure that involves a recursive application of itself to walk down the input list.
Part 3: Strings but not numbers
Now suppose that you are given a list consisting of either strings or numbers. Complete the following definition so that applying this procedure to such a list would result in the total length of the strings in the list, but not the numbers. (Note that predicates for checkings types, such as number? or string? may be helpful.) For example:
Use map, filter, or fold-right (with the definitions given in lecture) to do this. Please don't write a procedure that involves a recursive application of itself to walk down the input list.
Lec.6.5.1: Make a Multiplier
Write a procedure (make-multiplier x), of type number->(number->number), that returns a procedure that takes an argument and multiplies that argument by x. So
and
Lec.6.6.1: Higher order expressions
Remember that compose is defined as:
For each of the following expressions, if it is valid, indicate its value (if the value is a procedure, simply indicate "procedure"; if the value is unspecified, then indicate "unspecified"); if it is not valid, indicate "error". You may abbreviate "error", "procedure" and "unspecified" by their first letters, "e", "p" and "u" respectively.
((compose square (lambda (x) (+ x 2))) 3)
((compose (lambda (x) (+ x 2)) square) 3)
(lambda (x) (lambda (y) (x y)))
((lambda (x) (lambda (y) (x y))) square)
PS.3.2.1: Types with Variables
Indicate the type of each of the following expressions. If you need type variables, use A,B,C, etc., starting with A as the leftmost variable.
(lambda (x y) x)
(lambda (p) (p 3))
(lambda (p x) (p x))
(lambda (x y comp) (if (comp x y) x y))
(for simplicity, assume x and y are of the same type)
PS.3.2.2: Recursive count-true
Write a recursive (not iterative) procedure, (count-true pred lower upper), of type (number->boolean),number,number->number, that returns the number of integers in the range lower..upper (inclusive) for which predicate pred applied to that number is true.
PS.3.2.3: Iterative count-true
Write an iterative procedure, (count-true pred lower upper), of type (number->boolean),number,number->number, that returns the number of integers in the range lower..upper (inclusive) for which predicate pred applied to that number is true. Note that you can type multiple defines into the code box below.
PS.3.2.4: Recursive accumulate-interval
Write a recursive procedure, (accumulate-interval op init lower upper), of type
that combines all of the elements in the interval lower upper by successive applications of op, starting with value init. So
would be equivalent to
and
would be equivalent to
PS.3.2.5: f and g
Suppose the following have been defined:
For each of the following expressions, indicate the result of evaluating it. Possible answers are: a numeric value, the word "procedure" followed by a space and an integer indicating the number of arguments the procedure takes, or "error".
We're matching your answer directly against the answer string, so please don't include extra spaces.
f
g
(* (f 3 2) 7)
(g 6)
(f 6)
((f 4 7) 5)
PS.3.2.6: Repeatedly Apply
Write a procedure (repeatedly-apply p n), of type (A->A),number->(A->A), that returns a procedure that takes a single argument and applies p to it n times. So, for example,
You may assume the following function has been defined (you don't have to use it, but it can contribute to a pretty solution.)
PS.3.2.7: Currying
Currying (named after the logician Haskell Curry) is the operation of taking a procedure of multiple arguments and turning it into a higher-order procedure. So a basic version of plus is defined
and used (plus 2 3). A curried version of plus has type number->(number->number), and is defined
and used
Write your own higher-order procedure, (curry p), of type (A,B->C)->(A->(B->C)), that takes a binary procedure and curries it. So, for example,
Part 3.3: Optional Problems
PS.3.3.1: Who you callin' odd?
Write a procedure (mix-it-up lst) that takes a list as input, and returns a new list, with the property that for each element of argument list that is odd, it doubles the element, and for each element that is even, it halves the element. You may assume that odd? exists as a predicate. Here are some examples:
PS.3.3.2: Recursive accumulate-filtered-interval
Write a procedure,
(accumulate-filtered-interval filter op init lower upper)
, of type(number->boolean),(number,number->number),number,number,number->number
that accumulates values as in accumulate-interval, but only those for which the filter predicate returns #t.PS.3.3.3: Counting Truth, Again
Write a new definition of count-true that just makes a single call to accumulate-filtered-interval.
The text was updated successfully, but these errors were encountered: