Exercises in this lecture              Go to the slide, where this exercise belongs -- Keyboard shortcut: 'u'  

Exercise 1.8
The get-prop function for property lists *


The R5RS Scheme functions assoc, assq, and assv access pairs in association lists. In this exercise we will write a similar function for property lists. More specifically, write a function get-prop with the following signature:

(get-prop key property-list)

The function should return the value of key in property-list. Like assoc, get-prop should return #f if key is not found in property-list. Here is an example REPL session with get-prop:

> (define weekday-plist (list 'monday 1 'tuesday 2 'wednesday 3 'thursday 4 'friday 5 'saturday 6 'sunday 7))
> (get-prop 'wednesday weekday-plist)
3
> (get-prop 'sunday weekday-plist)
7
> (get-prop 'january weekday-plist)
#f

How will you handle the case where the property list is malformed - a property list with an odd number of elements?

Discuss pros and cons of property lists and get-prop, compared to association lists and assoc.

Does the #f value, returned in cases where we do not find the key, bother you?


Solution