Functional Problem Solving (CSC 151 2016S) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Taking Notes] [Rubric]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Labs] [Outlines] [Readings] - [Examples] [Handouts]
Reference: [Setup] [Remote] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Curtsinger (2016S)] [Davis (2013F)] [Rebelsky (2015F)] [Weinman (2014F)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)]
Overview
(define reverse
(lambda (lst)
(if (null? lst)
null
(append (reverse (cdr lst)) (list (car lst))))))
Beware the jabberwok, and things that depend on the length of the list.
In this case, append depends on the length of the list.
The reverse procedure also has to step through the list. We are
going to have to deal with recursive procedures if we want to process
a whole list.
But it's dangerous to call append at every step. Append is going
to do a lot of work at every step.
Example with five element list.
> (reverse '(1 2 3 4 5))
> (append (reverse '(2 3 4 5)) '(1)) ; Sam skip steps
> (append (append (reverse '(3 4 5)) '(2)) '(1)) ; Sam skip steps
> (append (append (append (reverse '(4 5)) '(3)) '(2)) '(1)) ; Sam skip step))
> (append (append (append (append (reverse '(5)) '(4)) '(3)) '(2)) '(1)) ; Sam skip step
> (append (append (append (append (append (reverse '()) '(5)) '(4)) '(3)) '(2)) '(1)) ; Sam skip step
> (append (append (append (append (append '() '(5)) '(4)) '(3)) '(2)) '(1)) ;
> (append (append (append (append '(5) '(4)) '(3)) '(2)) '(1)) ; 1 call to append
; We remember that (append (list-of-length-k) lst) represents k+1 calls
> (append (append (append '(5 4)) '(3)) '(2)) '(1)) ; 2 more calls to append; 3 total
> (append (append '(5 4 3) '(2)) '(1)) ; 3 more calls to append; 6 total
> (append '(5 4 3 2) '(1)) ; 4 more calls to append; 10 total
> '(5 4 3 2 1) ; 5 more calls to append; 15 total
If the list had six elements, it would have been 6 more or 21 total
If the list had seven elements, it would have been 7 more or 28 total
If the list had twenty elements, it would have been 1 + 2 + 3 + 4 + 5 + ... + 20.
Computer scientists like to look at how efficient algorithms are. We saw two reversing algorithms. It turns out that one was much better than the other.
So we like to try to identify the general cost of an algorithm.
car takes the same amount of time, whether the list is 1 element
or 10,000,000,000. (If you think about the diagram, you just have
to follow one arrow.)Once we've analyzed an algorithm, we ask "Can we do this better."
How can I tell there is a mentor in my set of cards?
Look through the cards one by one until a. You find Sarah OR b. You run out of cards
In Scheme
(define find-mentor
(lambda (lst)
(cond
[(null? lst)
(error "Your mentor is missing.")]
[(mentor? (car lst))
(car lst)]
[else
(find-mentor (cdr lst))])))
If there are N students in my set of cards, how long will this take?
a. Best case? It's first: Some constant (null? + car + mentor?), but pretty fast.
b. Worst case? Go through the entire stack. Some constant times n.
c. Average case (whatever that is)?
How would I make this more efficient?
A valid critique: This assumes that you can easily find the middle element and throw away half. We don't yet know how to do that.
Throw away half; down to 1
Throw away half; down to
Suppose we represent each course at the College as a list of the form
'("ID" YEAR SEMESTER "TITLE" "PROF" "ROOM" "TIMES")`
For example, this course would be represented by the following list.
'("CSC 151.02" 2016 Spring "Functional Problem Solving"
"Rebelsky" "Science 3813" "MTWF 3:00-3:50")
Suppose also that we are storing classes in an array and want to search for classes by class ID.
Write 6P-style documentation for binary search (that is, specify inputs, outputs, and expectations). Your procedure should accept different arrays of courses.
;;; Procedure:
;;; binary-search-courses
;;; Parameters:
;;; courses, an array of courses
;;; Purpose:
;;;
;;; Produces:
;;;
;;; Preconditions:
;;; Each course in courses is a list of the form
;;; '("ID" YEAR SEMESTER "TITLE" "PROF" "ROOM" "TIMES"),
;;; where YEAR is an integer and SEMESTER is a symbol
;;; ('Fall, 'Spring, or 'Summer).
;;;
;;; Postconditions:
;;;