I'm supposed to be writing a function that receives a key and an association
list of this form:
(setf jane '((jane address
(number 1201)
(street main)
(city san-antonio)
(state texas)
(zip 75438))
(parents
(father pete)
(mother sue))
(children bob)
(name jane)
(weight 120)))
---------------------------
I need to display the data under the key (e.g. (assockey parents jane) would
return ((father pete) (mother sue))
Here is what I have as my function:
(defun assockey (keyinput person)
(rest (assoc keyinput person)))
but the error message I get is:
Error in EVAL: Symbol has no value: PARENTS
Control Stack Debugger
Eval #6: PARENTS
They 'keyinput' variable is where I am having my problem. Any hints on how
to get this?
I'm a beginner with Lisp (as if you couldn't tell), so go easy on me :)
Thanks,
JD
> I'm supposed to be writing a function that receives a key and an association
> list of this form:
[snip]
> I need to display the data under the key (e.g. (assockey parents jane) would
> return ((father pete) (mother sue))
Is that the exact specification? Think about what it means to say
(assockey parents jane)
Lisp will look at the first argument as the name of the function.
When it evaluates the other two arguments, it will take them as
variable names.
We saw you define JANE as a variable, but is PARENTS defined as a
variable? That's what the message is saying.
I think you may be looking in the wrong place for the problem: you're
looking at your function, but it seems that the problem crops up
before the function gets called. (If you run "(trace assockey)" this
will become more apparent... at least, calls to ASSOCKEY will be
notably absent.)
So, what's the proper calling sequence? How do you keep Lisp from
evaluating PARENTS as a variable? (Hint: You used it in your posting,
to keep a list from being evaluated as a function call.)
Hope this helps,
joelh
"JD" <·············@yahoo.com> writes:
> I need to display the data under the key (e.g. (assockey parents jane) would
> return ((father pete) (mother sue))
>
> Here is what I have as my function:
>
> (defun assockey (keyinput person)
> (rest (assoc keyinput person)))
>
> but the error message I get is:
>
> Error in EVAL: Symbol has no value: PARENTS
Try quoting the key; in your example, use
(assockey 'parents jane)
--
vsync
http://quadium.net/
(cons (cons (car (cons 'c 'r)) (cdr (cons 'a 'o))) ; Orjner
(cons (cons (car (cons 'n 'c)) (cdr (cons nil 's))) nil))
Seems like you need to quote the symbol PARENTS so it doesn't get evaluated,
ie. (assockey 'parents jane)
Think of the difference between the symbol JANE and the value it is bound to,
and then consider that PARENTS is not even bound to any value (nor does it
name any variable).
--
; Matthew Danish <·······@andrew.cmu.edu>
; OpenPGP public key: C24B6010 on keyring.debian.org
; Signed or encrypted mail welcome.
; "There is no dark side of the moon really; matter of fact, it's all dark."
DOH!
All I had to do was add the ' in front of the middle variable when calling
the function.
JD
"JD" <·············@yahoo.com> wrote in message
···························@typhoon.austin.rr.com...
> I'm supposed to be writing a function that receives a key and an
association
> list of this form:
>
> (setf jane '((jane address
> (number 1201)
> (street main)
> (city san-antonio)
> (state texas)
> (zip 75438))
> (parents
> (father pete)
> (mother sue))
> (children bob)
> (name jane)
> (weight 120)))
> ---------------------------
>
> I need to display the data under the key (e.g. (assockey parents jane)
would
> return ((father pete) (mother sue))
>
> Here is what I have as my function:
>
> (defun assockey (keyinput person)
> (rest (assoc keyinput person)))
>
> but the error message I get is:
>
> Error in EVAL: Symbol has no value: PARENTS
>
> Control Stack Debugger
> Eval #6: PARENTS
>
> They 'keyinput' variable is where I am having my problem. Any hints on
how
> to get this?
>
> I'm a beginner with Lisp (as if you couldn't tell), so go easy on me :)
>
> Thanks,
>
> JD
>
>
I usually do this type of access
with a property list and getf.
LISP(6): (setf jill '(:address (:number 1201
:street main
:city san-antonio
:state texas
:zip 75438)
:parents (:father pete
:mother sue)
:children bob
:name jill
:weight 120))
LISP(7): (getf jill :parents)
(:FATHER PETE :MOTHER SUE)
LISP(8): (getf (getf jill :parents) :father)
PETE
LISP(9): (setf (getf jill :weight) 130) ;;; change jill's weight
130
Thanks,
Jeff Sandys
JD wrote:
>
> I'm supposed to be writing a function that
> receives a key and an association
> list of this form:
>
> (setf jane '((jane address
> (number 1201)
> (street main)
> (city san-antonio)
> (state texas)
> (zip 75438))
> (parents
> (father pete)
> (mother sue))
> (children bob)
> (name jane)
> (weight 120)))
Jeff Sandys <·······@juno.com> writes:
> LISP(6): (setf jill '(:address (:number 1201
> :street main
> :city san-antonio
> :state texas
> :zip 75438)
> :parents (:father pete
> :mother sue)
> :children bob
> :name jill
> :weight 120))
>
> LISP(7): (getf jill :parents)
> (:FATHER PETE :MOTHER SUE)
> LISP(8): (getf (getf jill :parents) :father)
> PETE
> LISP(9): (setf (getf jill :weight) 130) ;;; change jill's weight
> 130
The important thing to note here is that using keywords instead of
symbols means you don't have to worry about accidentally unquoted
symbols being evaluated. Keywords always evaluate to themselves.
Some people favor the use of keywords for data delimiters like this.
Others claim that keywords should only be used for keyword arguments
to functions (and in similar cases). On the third hand, some claim
that using keywords for data delimiters in this manner *is* consistent
with their use in lambda lists for function (etc) arguments. This is
a stylistic issue that's up to the individual to decide, since the
language doesn't really care either way.
'james
--
James A. Crippen <·····@unlambda.com> ,-./-. Anchorage, Alaska,
Lambda Unlimited: Recursion 'R' Us | |/ | USA, 61.20939N, -149.767W
Y = \f.(\x.f(xx)) (\x.f(xx)) | |\ | Earth, Sol System,
Y(F) = F(Y(F)) \_,-_/ Milky Way.
>> LISP(6): (setf jill '(:address (:number 1201
[snip]
> The important thing to note here is that using keywords instead of
> symbols means you don't have to worry about accidentally unquoted
> symbols being evaluated. Keywords always evaluate to themselves.
> Some people favor the use of keywords for data delimiters like this.
[snip]
> Others claim that keywords should only be used for keyword arguments
[snip]
My preference is to not use keywords in such a scenario, simply so
that other libraries can store their data in the same structure too
without a namespace conflict. Kinda like plists in some respects.
But I don't really feel strongly one way or another. Certainly not
enough to criticize the guy who actually writes the code for his
choice.
Cheers,
joelh
Joel Ray Holveck <·····@juniper.net> writes:
> >> LISP(6): (setf jill '(:address (:number 1201
> [snip]
> > The important thing to note here is that using keywords instead of
> > symbols means you don't have to worry about accidentally unquoted
> > symbols being evaluated. Keywords always evaluate to themselves.
> > Some people favor the use of keywords for data delimiters like this.
> [snip]
> > Others claim that keywords should only be used for keyword arguments
> [snip]
>
> My preference is to not use keywords in such a scenario, simply so
> that other libraries can store their data in the same structure too
> without a namespace conflict. Kinda like plists in some respects.
>
> But I don't really feel strongly one way or another. Certainly not
> enough to criticize the guy who actually writes the code for his
> choice.
Well, I'd criticize someone who wrote code like that if I had to share
data structures with it -- otherwise, if it's safely in a single
module, it's okay I guess. Of course, I'd probably use classes for
this instead, where you /have/ to use non-keyword symbols for slot
names (thankfully).
--
/|_ .-----------------------.
,' .\ / | No to Imperialist war |
,--' _,' | Wage class war! |
/ / `-----------------------'
( -. |
| ) |
(`-. '--.)
`. )----'
Jeff Sandys <·······@juno.com> writes:
> I usually do this type of access
> with a property list and getf.
>
> LISP(6): (setf jill '(:address (:number 1201
> :street main
> :city san-antonio
> :state texas
> :zip 75438)
> :parents (:father pete
> :mother sue)
> :children bob
> :name jill
> :weight 120))
>
> LISP(7): (getf jill :parents)
> (:FATHER PETE :MOTHER SUE)
> LISP(8): (getf (getf jill :parents) :father)
> PETE
> LISP(9): (setf (getf jill :weight) 130) ;;; change jill's weight
> 130
>
> > I'm supposed to be writing a function that
> > receives a key and an association
> > list of this form:
> >
> > (setf jane '((jane address
> > (number 1201)
> > (street main)
> > (city san-antonio)
> > (state texas)
> > (zip 75438))
> > (parents
> > (father pete)
> > (mother sue))
> > (children bob)
> > (name jane)
> > (weight 120)))
Shouldn't there be a copy-list or something similar in here somewhere
so that literals aren't being modified?