Download presentation
Presentation is loading. Please wait.
Published byMaximillian Hall Modified over 9 years ago
1
CS116 Tutorial 1 Review of CS115
2
Reminders Assignment 1 is due Wednesday, January 21th at 12pm (Noon) Submit Assignment 0 if you have not done so already
3
Review Local Map Filter Foldr
4
Local ( local [ (define …) (define …) … ] ;; main body of function here ;; ) Allows your main function to quickly access the functions/constants defined inside local All functions/constants can the main function’s parameters One set of square brackets
5
Map (map f lst) ;;map:(X -> Y) (listof X) -> (listof Y) Produces a list of values by applying f to each value in lst ;;f: X -> Y Same type! This is an element of lst, not lst itself
6
What do you notice about the result of using map? The result is always a list Length of the produced list does not change (map f (list x y z … )) => (list (f x) (f y) (f z) … )
7
Filter (filter f lst) ;;filter:(X -> Bool) (listof X) -> (listof X) Produces a list of values in lst that produces true when f is applied to them ;;f: X -> Bool Same type! This is an element of lst, not lst itself
8
What do you notice about the result of using filter? The result is always a list Length of the produced list may or may not change
9
Foldr (foldr combine base lst) ;;foldr:(X Y -> Y) Y (listof X) -> Y Produces the result of applying combine through the lst or produces base if it is empty Same type!
10
Foldr ;;combine: X Y -> Y (combine x (combine y (combine …(combine z base) The right most expression gets applied to first! Element in lst The “result so far” of applying combine
11
What do you notice about the result of using foldr? The result can be any type ◦ Numbers ◦ Lists ◦ Strings ◦ Etc.
12
(define-struct foodcan (height radius weight label)) ;; a Foodcan is (make-foodcan Num Num Num Sym) ;; requires: ;; * height > 0 is height of can in cm ;; * radius > 0 is radius of can in cm ;; * weight > 0 is the weight of can in grams ;; * label indicates contents of can. (define tomatocan (make-foodcan 10 4 400 ‘tomatoes)) (define beancan (make-foodcan 9 3 200 ‘beans))
13
Question 1 Using abstract list functions, write a Scheme function supersize-it that consumes a list of foodcan, cans, and produces a new list from it. The height and weight of each new can is twice that of the corresponding consumed foodcan, and the radius and label are unchanged. (define (supersize-it cans) …)
14
Question 1
15
Question 2 Using abstract list functions, write a function large-cans that consumes a list of foodcan structures, cans, and a non- negative number, size, and produces a list of the labels on all the cans with weight at least as much as the consumed value. (define (large-cans cans size) …)
16
Question 2
17
Question 3 Using abstract list functions, write a function total-volume to compute the total volume of the cans in a list of foodcan. (define (total-volume cans) …)
18
Question 3
19
Question 4 Write a function num-above-avg which consumes a list of number, values, and produces the number of values in the list which exceed the average value in list. If the consumed list is empty or of length 1, 0 will be produced. (define (num-above-avg values) …)
20
Question 4
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.