I need a little help with my car/cdr recursion that doesn't do what I
need:
consider this data-file
(((NAME "george") (FAMILYNAME "bush") (AGE "29") (TLF "555-432343"))
((NAME "cathrine") (FAMILYNAME "bush") (AGE "18") (TLF "555-55432"))
((NAME "george") (FAMILYNAME "mc donnald") (AGE "32") (TLF
"555-22222") (TLF "555-33333")))
(defun find-value (value list)
(and (consp value) (eq value (car list))))
(defun extract (category value list)
(cond ((null list) nil)
((and (consp list) (eq (car list) category))
(cons (remove-if-not #'find-value list)
(extract category (cdr list))))))
the function extract should get
(extract 'tlf 'george data-file) and give us all the tlf. numbers of
all georges in our data-file
ab talebi
············@yahoo.com (ab talebi) writes:
> I need a little help with my car/cdr recursion that doesn't do what I
> need:
>
> consider this data-file
> (((NAME "george") (FAMILYNAME "bush") (AGE "29") (TLF "555-432343"))
> ((NAME "cathrine") (FAMILYNAME "bush") (AGE "18") (TLF "555-55432"))
> ((NAME "george") (FAMILYNAME "mc donnald") (AGE "32") (TLF
> "555-22222") (TLF "555-33333")))
>
> (defun find-value (value list)
> (and (consp value) (eq value (car list))))
>
> (defun extract (category value list)
> (cond ((null list) nil)
> ((and (consp list) (eq (car list) category))
> (cons (remove-if-not #'find-value list)
> (extract category (cdr list))))))
>
> the function extract should get
> (extract 'tlf 'george data-file) and give us all the tlf. numbers of
> all georges in our data-file
I answered this before. The answer is the same.
remove-if-not takes one-place predicates. e.g., oddp.
(remove-if-not #'oddp '(1 2 3 4)) => (1 3)
You're using find-value as the predicate and it takes two arguments.
"Currying" would help you here if htis was the right approach.
REMOVE-IF-NOT is simply not the right way to go, though. If you're
allowed to use that in homework (which I assume this is) then you should
be allowed to use FIND as well. Or ASSOC. See CLHS for info on these.
Removing all of the elements of a list but one and trying to use what remains
is simply the wrong way to search a list for a value to use...
On Sat, 17 Nov 2001 22:29:26 GMT, Kent M Pitman <······@world.std.com>
wrote:
>············@yahoo.com (ab talebi) writes:
>
>> I need a little help with my car/cdr recursion that doesn't do what I
>> need:
>>
>> consider this data-file
>> (((NAME "george") (FAMILYNAME "bush") (AGE "29") (TLF "555-432343"))
>> ((NAME "cathrine") (FAMILYNAME "bush") (AGE "18") (TLF "555-55432"))
>> ((NAME "george") (FAMILYNAME "mc donnald") (AGE "32") (TLF
>> "555-22222") (TLF "555-33333")))
>>
>> (defun find-value (value list)
>> (and (consp value) (eq value (car list))))
>>
>> (defun extract (category value list)
>> (cond ((null list) nil)
>> ((and (consp list) (eq (car list) category))
>> (cons (remove-if-not #'find-value list)
>> (extract category (cdr list))))))
>>
>> the function extract should get
>> (extract 'tlf 'george data-file) and give us all the tlf. numbers of
>> all georges in our data-file
>
>Or ASSOC. See CLHS for info on these.
>Removing all of the elements of a list but one and trying to use what remains
>is simply the wrong way to search a list for a value to use...
it makes perfect sense I don't have to remove each element in order to
extract the peace of information that I want, so I changed my extract
function to use ASSOC instead of REMOVE-IF-NOT and I have:
(defun extract (category value list)
(cond ((null list) nil)
((and (consp list) (eq (car list) category))
(cons (assoc value list)
(extract category (cdr list))))))
still (extract 'name 'george 'data-file) returns NIL :(
ab talebi
On Sun, 18 Nov 2001 13:03:50 GMT, ············@yahoo.com (ab talebi)
wrote:
>On Sat, 17 Nov 2001 22:29:26 GMT, Kent M Pitman <······@world.std.com>
>wrote:
>
>>············@yahoo.com (ab talebi) writes:
>>
>>> I need a little help with my car/cdr recursion that doesn't do what I
>>> need:
>>>
>>> consider this data-file
>>> (((NAME "george") (FAMILYNAME "bush") (AGE "29") (TLF "555-432343"))
>>> ((NAME "cathrine") (FAMILYNAME "bush") (AGE "18") (TLF "555-55432"))
>>> ((NAME "george") (FAMILYNAME "mc donnald") (AGE "32") (TLF
>>> "555-22222") (TLF "555-33333")))
>>>
>>> (defun find-value (value list)
>>> (and (consp value) (eq value (car list))))
>>>
>>> (defun extract (category value list)
>>> (cond ((null list) nil)
>>> ((and (consp list) (eq (car list) category))
>>> (cons (remove-if-not #'find-value list)
>>> (extract category (cdr list))))))
>>>
>>> the function extract should get
>>> (extract 'tlf 'george data-file) and give us all the tlf. numbers of
>>> all georges in our data-file
>>
>>Or ASSOC. See CLHS for info on these.
>>Removing all of the elements of a list but one and trying to use what remains
>>is simply the wrong way to search a list for a value to use...
>
>it makes perfect sense I don't have to remove each element in order to
>extract the peace of information that I want, so I changed my extract
>function to use ASSOC instead of REMOVE-IF-NOT and I have:
>
>(defun extract (category value list)
> (cond ((null list) nil)
> ((and (consp list) (eq (car list) category))
> (cons (assoc value list)
> (extract category (cdr list))))))
>
>still (extract 'name 'george 'data-file) returns NIL :(
>
>ab talebi
oobs my mistake, it should be (extract 'tlf 'george data-file)
let me try that.........
no, .... the same result it returns NIL and I expected it to return
all the tlf. numbers of all the persons with the name = george