[Current] [News] [Glance] [Discussions] [Instructions] [Search] [Links] [Handouts] [Outlines] [Readings] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [Fall2000.01] [Spring2000]
Back to Scheme without DrScheme. On to CGI Scripting.
Held Wednesday, September 27, 2000
Summary
Today we revisit the issue of searching lists.
Notes
display
or read
on the exam.
Overview
c. Define and test a procedure that takes two arguments, the first an atom and the second an association list whose keys are all lists of atoms. The procedure should return a list of all the values whose keys contain the atom.
(define olympic-events (list (cons (list 'individual 'exciting 'dangerous 'water) "High Dive") (cons (list 'dangerous 'new 'gymnastics 'individual) "Trampoline") (cons (list 'team 'popular) "Volleyball") (cons (list 'water 'team) "Water Polo")))
(search-by-adjective adj list)
(define search-by-adjective (lambda (adj stuff) ...))
cond
(cond ; Base case: The list of things is empty so return the empty list ((...) ...) ; Successful case: The adjective describes the first value ; so return the first value plus other matching values. ((...) ...) ; Default case: Unsuccessful match. Try the rest. (else ...))
(null? stuff)
null
adj
(caar stuff)
(cons (cdaar stuff) (search-by-adjective adj (cdr stuff)))
(define search-by-adjective (lambda (adj stuff) (cond ; Base case: The list of things is empty so return the empty list ((null? stuff) null) ; Successful case: The adjective describes the first value ; so return the first value plus other matching values. ((member? adj (caar stuff)) (cons (cdar stuff) (search-by-adjective adj (cdr stuff)))) ; Default case: Unsuccessful match. Try the rest. (else (search-by-adjective adj (cdr stuff)))))) (define member? (lambda (val lst) (cond ; You can't be a member of the empty list. ((null? lst) #f) ; If you're the first thing in a list, then you're a member ; of that list. ((equal? val (car lst)) #t) ; Otherwise, check the remainder of the list. (else (member? val (cdr lst))))))
> (search-by-adjective 'exciting olympic-events) ("High Dive") > (search-by-adjective 'water olympic-events) ("High Dive" "Water Polo") > (search-by-adjective 'silly olympic-events) ()
Thursday, 24 August 2000
Wednesday, 27 September 2000
Friday, 29 September 2000
Back to Scheme without DrScheme. On to CGI Scripting.
[Current] [News] [Glance] [Discussions] [Instructions] [Search] [Links] [Handouts] [Outlines] [Readings] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [Fall2000.01] [Spring2000]
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2000F/Outlines/outline.20.html
Source text last modified Fri Sep 29 10:31:33 2000.
This page generated on Fri Sep 29 10:42:03 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu