Strange behaviour of (push )
Posted: Wed Nov 05, 2003 11:04 pm
I have a database organised as association lists (relations) of registers, one for each individual, and each register itself contains an association list of pairs <field value>. So, for example, it would be:
(set 'people
'(p1 (name (john macy)) (age 45) (hobbies fishing reading jogging))
(p2 (name (mary dickens)) (age 27) (hobbies knitting dancing watching-tv))
(p3 (name (paul donkey)) (age 18) (hobbies kicking eating)))
Now, I need to be able to insert a value into the some field, for some person-register:
(define (insert value field-id register-id relation , register field new-field new-register)
(set 'register (assoc register-id relation))
(set 'field (assoc field-id register))
(set 'new-field (push value field 1))
(set 'new-register (replace-assoc field-id register new-field))
(replace-assoc register-id relation new-register))
It responds with an error message "list expected in function replace-assoc". It seems that 'push' does not return a list. (It makes no difference to use or not the second argument to push; it always returns the atom <value>.
When I replace '(push value field 1)' for '(cons (first field) (cons value (rest field)))' it works smoothly.
Any suggestions ?
BTW, how would you code it ?
(set 'people
'(p1 (name (john macy)) (age 45) (hobbies fishing reading jogging))
(p2 (name (mary dickens)) (age 27) (hobbies knitting dancing watching-tv))
(p3 (name (paul donkey)) (age 18) (hobbies kicking eating)))
Now, I need to be able to insert a value into the some field, for some person-register:
(define (insert value field-id register-id relation , register field new-field new-register)
(set 'register (assoc register-id relation))
(set 'field (assoc field-id register))
(set 'new-field (push value field 1))
(set 'new-register (replace-assoc field-id register new-field))
(replace-assoc register-id relation new-register))
It responds with an error message "list expected in function replace-assoc". It seems that 'push' does not return a list. (It makes no difference to use or not the second argument to push; it always returns the atom <value>.
When I replace '(push value field 1)' for '(cons (first field) (cons value (rest field)))' it works smoothly.
Any suggestions ?
BTW, how would you code it ?