Functional Problem Solving (CSC 151 2015F) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Taking Notes] [Rubric] [Remote Access]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Labs] [Outlines] [Readings] - [Examples] [Handouts]
Reference: [Setup] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Curtsinger (2015F)] [Davis (2013F)] [Rebelsky (2015S)] [Weinman (2014F)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)]
Overview
Topics
Some things to know about
Types of Questions
There are enough of these that you should check the eboard.
Without using list-ref, write a procedure, (my-list-ref lst n),
that extracts element n of a list. For example,
> (my-list-ref (list "red" "orange" "yellow" "green" "blue" "indigo" "violet") 5)
"indigo"
> (my-list-ref (list "red" "orange" "yellow" "green" "blue" "indigo" "violet") 0)
"red"
Check your answer!
(define my-list-ref (lambda (lst n) (display (list 'my-list-ref lst n)) (newline) (if (zero? n) (car lst) (my-list-ref (cdr lst) (- n 1)))))
(my-list-ref (list "red" "orange" "yellow" "green" "blue" "indigo" "violet") 0) (my-list-ref (red orange yellow green blue indigo violet) 0) "red" (my-list-ref (list "red" "orange" "yellow" "green" "blue" "indigo" "violet") 1) (my-list-ref (red orange yellow green blue indigo violet) 1) (my-list-ref (orange yellow green blue indigo violet) 0) "orange" (my-list-ref (list "red" "orange" "yellow" "green" "blue" "indigo" "violet") 5) (my-list-ref (red orange yellow green blue indigo violet) 5) (my-list-ref (orange yellow green blue indigo violet) 4) (my-list-ref (yellow green blue indigo violet) 3) (my-list-ref (green blue indigo violet) 2) (my-list-ref (blue indigo violet) 1) (my-list-ref (indigo violet) 0) "indigo"
Identify the errors in the following procedure, which is supposed to count the number of odd values in a list of integers.
(define tally-odd
(lambda (lst)
(tally-odd-kernel 0 lst)))
(define tally-odd-helper
(lambda (lst tally)
(cond
[(null? lst)
0]
[(odd? (car lst))
(+ 1 tally)]
[else
(tally-odd helper lst tally)])))
tally-odd-kernel to tally-odd-helpertally-odd helper should be tally-odd-helper.(tally-odd-helper (cdr lst) (+ 1 tally)).else clause, we want the cdr of the list.tally.Consider the following procedure
(define fun
(lambda (lst)
(if (null? lst)
0
(- (car lst) (fun (cdr lst))))))
Show the steps in evaluating (fun '(1 2 3 4 5)). You need not show
the evaluation of if, car, and cdr; just show the results. For
example,
(fun '(1 2 3 4 5))
=> (- 1 (fun '(2 3 4 5)))
Consider the following procedure
(define fun
(lambda (lst)
(funner (cdr lst) (car lst))))
(define funner
(lambda (lst val)
(if (null? lst)
val
(funner (cdr lst) (- val (car lst))))))
Show the steps in evaluatng (fun '(1 2 3 4 5). You can immediately
evaluate car, cdr, and if.
Finish the instructions for the tally-odd procedure below.
(define tally-odd
(lambda (lst)
(cond
[(null? lst)
_____]
[(odd? (car lst))
_____ (tally-odd (cdr lst))]
[else
_____ (tally-odd (cdr lst))])))
(define tally-odd
(lambda (lst)
(cond
[(null? lst)
0]
[(odd? (car lst))
(+ 1 (tally-odd (cdr lst)))]
[else
(tally-odd (cdr lst))])))
Would also accept the last line as the following. (But it's somewhat silly to add 0, it's just extra code and extra work.)
(+ 0 (tally-odd (cdr lst)))])))
Fill in the Six P's for the following procedure
(define whatzitdo
(lambda (lst)
(whatzitdotwo lst null)))
(define whatzitdotwo
(lambda (lst val)
(if (null? lst)
val
(whatzitdotwo (cdr lst) (cons (car lst) val)))))
Consider the following procedure
(define whatzitdo
(lambda (lst)
(whatzitdotwo lst null null)))
(define whatzitdotwo
(lambda (lst foo bar)
(if (null? lst)
(list (reverse foo) (reverse bar))
(whatzitdotwo (cdr lst)
(cons (car lst) bar)
foo))))
What is the value of (whatzitdo '(1 2 3 4 5))?
(whatzitdo '(1 2 3 4 5))
=> (w2 '(1 2 3 4 5) '() '())
=> (w2 '(2 3 4 5) '(1) '())
=> (w2 '(3 4 5) '(2) '(1))
=> (w2 '(4 5) '(3 1) '(2))
=> (w2 '(5) '(4 2) '(3 1))
=> (w2 '() '(5 3 1) '(4 2))
=> '((1 3 5) (2 4))
; It split the list in half, using alternating positions!
Consider the following procedure.
(define fun (lambda (a b) (if (> a b) null (cons a (fun (+ a 1) b)))))
Show the steps in evaluating (fun 5 9).
(fun 5 9) (cons 5 (fun 6 9)) (cons 5 (cons 6 (fun 7 9))) (cons 5 (cons 6 (cons 7 (fun 8 9)))) (cons 5 (cons 6 (cons 7 (cons 8 (fun 9 9))))) (cons 5 (cons 6 (cons 7 (cons 8 (cons 9 (fun 10 9)))))) (cons 5 (cons 6 (cons 7 (cons 8 (cons 9 null))))) (cons 5 (cons 6 (cons 7 (cons 8 '(9))))) (cons 5 (cons 6 (cons 7 '(8 9)))) (cons 5 (cons 6 '(7 8 9)))) (cons 5 '(6 7 8 9)) '(5 6 7 8 9))
Consider the following procedure.
(define fun (lambda (a b) (if (> a b) null (cons a (fun (+ a 1) b)))))
What is the result of (fun -3 8)?
'(-3 -2 -1 0 1 2 3 4 5 6 7 8)
Document the following procedure, which takes two integers as input.
(define fun (lambda (a b) (if (> a b) null (cons a (fun (+ a 1) b)))))