
January 30th, 2012, 04:23 PM
|
|
Registered User
|
|
Join Date: Jan 2012
Posts: 1
Time spent in forums: 58 m 4 sec
Reputation Power: 0
|
|
|
Scheme lookup-function
Hello, everyone. I have a problem with scheme or, more precisely, with writing a script that contains a lookup-function that searches for a key in a pre-defined list and prints the value of the key (pair).
My problem is, that i have to do it with the define-struct-construction.
My script (until now):
PHP Code:
(define-struct pair (key value))
(define table (list
(make-pair 'o 3)
(make-pair 'r 5)
(make-pair 'x 9)))
(define (lookup key lst)
(let ((pair (assoc key lst)))
(if pair
(cadr pair)
(error key " not in list."))))
(lookup 'o table)
And my error message:
PHP Code:
assoc: non-pair found in list: (make-pair 'o 3) in
(list (make-pair 'o 3) (make-pair 'r 5) (make-pair 'x 9))
The program should find the pairs but instead tells me that there are not even pairs ("non-pairs"); I guess I do not understand how to use define-struct pair correctly. I searched the forum and like 5 hours or so on google and a LOT of other websites. I appreciate your help and hope you understand my problem. Sorry for bad English - Non-native speaker 
|