From: zion_zii
Subject: recursion problem
Date: 
Message-ID: <1133472091.984656.198410@g14g2000cwa.googlegroups.com>
Hi to all!
I attempted some end of chapter exercises and got stuck with this one
below. Please help me with HINTS so I can understand HOW TO solve it.

Given two lists:
lst1 = (bananas kiwis)
lst2 = (kiwis pears plums bananas cherries)

Write a function occurN that counts how many times an atom in lst1 also
occurs in lst2.
An output using the above lists would look like: (occurN lst1 lst2) is
2.
This is what I have

(defun occurN (lst1 lst2)
  (cond
    ( (or
       (null lst1)
       (null lst2) )
     0)
    (t (cond
	 ( (eq  (car lst1) (car lst2) )
	  (1+ (occurN lst1 (cdr lst2) ) ) )      ;atom may appear more than
once
	 ( (occurN lst1 (cdr lst2) ) )             ;increment lst2
	 (t  (occurN (cdr lst1)  lst2) ) ) ) ) )

My function gives me 1. I know that its only processing the first
element in list1 and terminating when list2 is null but I dont know how
to extend it so that it also processes the rest of the elements of
lst1. Any hints??

From:  (typep 'nil '(satisfies identity)) => ?
Subject: Re: recursion problem
Date: 
Message-ID: <1133478771.280597.316080@g47g2000cwa.googlegroups.com>
Hi!

(defun counter (list1 list2)
  (length (intersection list1 list2 :test #'eql)))

lisp is ... ;-)
From: Pascal Bourguignon
Subject: Re: recursion problem
Date: 
Message-ID: <877jangi86.fsf@thalassa.informatimago.com>
" (typep 'nil '(satisfies identity)) => ?" <··············@yahoo.it> writes:
> (defun counter (list1 list2)
>   (length (intersection list1 list2 :test #'eql)))

This isn't what was demanded.  It may happen that with some input, and
on some implementations, you get the same results, but the odds are
against you.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Cats meow out of angst
"Thumbs! If only we had thumbs!
We could break so much!"
From: Geoffrey Summerhayes
Subject: Re: recursion problem
Date: 
Message-ID: <7s7kf.5935$Et5.408419@news20.bellglobal.com>
"Pascal Bourguignon" <····@mouse-potato.com> wrote in message ···················@thalassa.informatimago.com...
>" (typep 'nil '(satisfies identity)) => ?" <··············@yahoo.it> writes:
>> (defun counter (list1 list2)
>>   (length (intersection list1 list2 :test #'eql)))
>
> This isn't what was demanded.  It may happen that with some input, and
> on some implementations, you get the same results, but the odds are
> against you.

Actually it's not that bad, the problem is
ill-defined if the input lists are not sets.
The phrasing of the question is ambiguous
as to exactly what is being counted.

--
Geoff
From:  (typep 'nil '(satisfies identity)) => ?
Subject: Re: recursion problem
Date: 
Message-ID: <1133705462.502687.255590@z14g2000cwz.googlegroups.com>
Pascal,just read again the first posting and you will find that it is
the right answer!


Given two lists:
lst1 = (bananas kiwis)
lst2 = (kiwis pears plums bananas cherries)

Write a function occurN that counts how many times an atom in lst1 also

occurs in lst2.



Especially when you assume that all elements of the two lists are
symbols.

We might discuss about the test function eq or eql to use, or
implementations that operate sligtly different, but don't ask me to
reference to all possible odds. 
the universe is huge, my time is short!
From: Pascal Bourguignon
Subject: Re: recursion problem
Date: 
Message-ID: <87iru4ewzo.fsf@thalassa.informatimago.com>
" (typep 'nil '(satisfies identity)) => ?" <··············@yahoo.it> writes:

> Pascal,just read again the first posting and you will find that it is
> the right answer!
>
>
> Given two lists:
> lst1 = (bananas kiwis)
> lst2 = (kiwis pears plums bananas cherries)
>
> Write a function occurN that counts how many times an atom in lst1 also
>
> occurs in lst2.
>
>
>
> Especially when you assume that all elements of the two lists are
> symbols.
>
> We might discuss about the test function eq or eql to use, or
> implementations that operate sligtly different, but don't ask me to
> reference to all possible odds. 
> the universe is huge, my time is short!

All right. This "also" indeed seems to indicate that the result must
be less or equal to the length of lst1.  I overlooked it and
understood the semantics I implemented in my first follow-up.  But I'm
not sure set semantics is what is expected, since the containers are
called lst, not set.

What result would be expected with: lst1 = (bananas kiwis bananas)?
or with: lst2 = (kiwis kiwis bananas pears plums bananas cherries)?

Once again, the only correct answer should have been:

     Specifications ambiguous or incomplete.  
     Please refine our specifications.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we. -- Georges W. Bush
From: Arthur Lemmens
Subject: Re: recursion problem
Date: 
Message-ID: <op.s04jqmfhwpmq96@news.xs4all.nl>
zion_zii <·········@gmail.com> wrote:

> I attempted some end of chapter exercises and got stuck with this one
> below. Please help me with HINTS so I can understand HOW TO solve it.
>
> Given two lists:
> lst1 = (bananas kiwis)
> lst2 = (kiwis pears plums bananas cherries)
>
> Write a function occurN that counts how many times an atom in lst1 also
> occurs in lst2.
> An output using the above lists would look like: (occurN lst1 lst2) is
> 2.

In your case (assuming just car, cdr, null, atom, etc.) I would start
by splitting the problem in two parts: first write a function, say
ATOM-IN-LIST-P that takes an atom and a list and returns T if the atom
is in the list (and NIL otherwise).  Then you can write your desired
function on top of that.
From: zion_zii
Subject: Re: recursion problem
Date: 
Message-ID: <1133474348.792314.20880@g44g2000cwa.googlegroups.com>
Hey thanks a whole lot. I dont know why I didnt think of it before but
I did as you suggested. I implemented
atom-in-list as follows:

(defun atom-in-list (key lst)
  (cond
    ( (null lst)  0)
    (t  (cond
	 ( (eq (car lst)  key)
	  (1+  (atom-in-list key (cdr lst) ) ) )
	 (t  (atom-in-list key (cdr lst) ) ) ) ) ) )

and then I called it within occurN as follows:

(defun occurN  (lst1 lst2)
  (cond
     ( (or
       (null lst1)
       (null lst2))
      0)
    (t  (+ (atom-in-list (car lst1)  lst2)
	  (occurN  (cdr lst1)  lst2) ) ) ) )

This gave me the right answer. Once again. Thanks a whole bunch.

"It is through such communities that real intellectual growth
manifests. The truth faces you elusively" -N.KIGS
From: Pascal Bourguignon
Subject: Re: recursion problem
Date: 
Message-ID: <87k6eoh29v.fsf@thalassa.informatimago.com>
"zion_zii" <·········@gmail.com> writes:

> Hey thanks a whole lot. I dont know why I didnt think of it before but
> I did as you suggested. I implemented
> atom-in-list as follows:
>
> (defun atom-in-list (key lst)
>   (cond
>     ( (null lst)  0)
>     (t  (cond
> 	 ( (eq (car lst)  key)
> 	  (1+  (atom-in-list key (cdr lst) ) ) )
> 	 (t  (atom-in-list key (cdr lst) ) ) ) ) ) )
>
> and then I called it within occurN as follows:
>
> (defun occurN  (lst1 lst2)
>   (cond
>      ( (or
>        (null lst1)
>        (null lst2))
>       0)
>     (t  (+ (atom-in-list (car lst1)  lst2)
> 	  (occurN  (cdr lst1)  lst2) ) ) ) )
>
> This gave me the right answer. Once again. Thanks a whole bunch.

Well, if you call another function like this, you could as well use
the existing Common Lisp functions.

(defun occur-n (elements list) (count-if (lambda (x) (member x elements)) list))

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

"You question the worthiness of my code? I should kill you where you
stand!"
From: Alejandro Guerra-Hernandez
Subject: Re: recursion problem
Date: 
Message-ID: <1134008609.350264.280050@g47g2000cwa.googlegroups.com>
Here is my solution using apply and mapcar :

CL-USER> (defun occurN (lst1 lst2)
	   (apply #'+ (mapcar #'(lambda (e) (count e lst2))
			      lst1)))
OCCURN
CL-USER> (occurn '(1 2) '(1 2 1 2 1 2))
6
CL-USER> (occurn '() '(a))
0
CL-USER> (occurn '(a) '())
0
CL-USER> (occurn '(bananas kiwis) '(kiwis pears plums bananas
cherries))
2

zion_zii skrev:

> Hi to all!
> I attempted some end of chapter exercises and got stuck with this one
> below. Please help me with HINTS so I can understand HOW TO solve it.
>
> Given two lists:
> lst1 = (bananas kiwis)
> lst2 = (kiwis pears plums bananas cherries)
>
> Write a function occurN that counts how many times an atom in lst1 also
> occurs in lst2.
> An output using the above lists would look like: (occurN lst1 lst2) is
> 2.
> This is what I have
>
> (defun occurN (lst1 lst2)
>   (cond
>     ( (or
>        (null lst1)
>        (null lst2) )
>      0)
>     (t (cond
> 	 ( (eq  (car lst1) (car lst2) )
> 	  (1+ (occurN lst1 (cdr lst2) ) ) )      ;atom may appear more than
> once
> 	 ( (occurN lst1 (cdr lst2) ) )             ;increment lst2
> 	 (t  (occurN (cdr lst1)  lst2) ) ) ) ) )
>
> My function gives me 1. I know that its only processing the first
> element in list1 and terminating when list2 is null but I dont know how
> to extend it so that it also processes the rest of the elements of
> lst1. Any hints??
From: Alejandro Guerra-Hernandez
Subject: Re: recursion problem
Date: 
Message-ID: <1134008630.594183.280810@g47g2000cwa.googlegroups.com>
Here is my solution using apply and mapcar :

CL-USER> (defun occurN (lst1 lst2)
	   (apply #'+ (mapcar #'(lambda (e) (count e lst2))
			      lst1)))
OCCURN
CL-USER> (occurn '(1 2) '(1 2 1 2 1 2))
6
CL-USER> (occurn '() '(a))
0
CL-USER> (occurn '(a) '())
0
CL-USER> (occurn '(bananas kiwis) '(kiwis pears plums bananas
cherries))
2

zion_zii skrev:

> Hi to all!
> I attempted some end of chapter exercises and got stuck with this one
> below. Please help me with HINTS so I can understand HOW TO solve it.
>
> Given two lists:
> lst1 = (bananas kiwis)
> lst2 = (kiwis pears plums bananas cherries)
>
> Write a function occurN that counts how many times an atom in lst1 also
> occurs in lst2.
> An output using the above lists would look like: (occurN lst1 lst2) is
> 2.
> This is what I have
>
> (defun occurN (lst1 lst2)
>   (cond
>     ( (or
>        (null lst1)
>        (null lst2) )
>      0)
>     (t (cond
> 	 ( (eq  (car lst1) (car lst2) )
> 	  (1+ (occurN lst1 (cdr lst2) ) ) )      ;atom may appear more than
> once
> 	 ( (occurN lst1 (cdr lst2) ) )             ;increment lst2
> 	 (t  (occurN (cdr lst1)  lst2) ) ) ) ) )
>
> My function gives me 1. I know that its only processing the first
> element in list1 and terminating when list2 is null but I dont know how
> to extend it so that it also processes the rest of the elements of
> lst1. Any hints??
From: Alejandro Guerra-Hernandez
Subject: Re: recursion problem
Date: 
Message-ID: <1134008655.418647.267940@g44g2000cwa.googlegroups.com>
Here is my solution using apply and mapcar :

CL-USER> (defun occurN (lst1 lst2)
	   (apply #'+ (mapcar #'(lambda (e) (count e lst2))
			      lst1)))
OCCURN
CL-USER> (occurn '(1 2) '(1 2 1 2 1 2))
6
CL-USER> (occurn '() '(a))
0
CL-USER> (occurn '(a) '())
0
CL-USER> (occurn '(bananas kiwis) '(kiwis pears plums bananas
cherries))
2

zion_zii skrev:

> Hi to all!
> I attempted some end of chapter exercises and got stuck with this one
> below. Please help me with HINTS so I can understand HOW TO solve it.
>
> Given two lists:
> lst1 = (bananas kiwis)
> lst2 = (kiwis pears plums bananas cherries)
>
> Write a function occurN that counts how many times an atom in lst1 also
> occurs in lst2.
> An output using the above lists would look like: (occurN lst1 lst2) is
> 2.
> This is what I have
>
> (defun occurN (lst1 lst2)
>   (cond
>     ( (or
>        (null lst1)
>        (null lst2) )
>      0)
>     (t (cond
> 	 ( (eq  (car lst1) (car lst2) )
> 	  (1+ (occurN lst1 (cdr lst2) ) ) )      ;atom may appear more than
> once
> 	 ( (occurN lst1 (cdr lst2) ) )             ;increment lst2
> 	 (t  (occurN (cdr lst1)  lst2) ) ) ) ) )
>
> My function gives me 1. I know that its only processing the first
> element in list1 and terminating when list2 is null but I dont know how
> to extend it so that it also processes the rest of the elements of
> lst1. Any hints??
From: Alejandro Guerra-Hernandez
Subject: Re: recursion problem
Date: 
Message-ID: <1134008677.764901.172240@f14g2000cwb.googlegroups.com>
Here is my solution using apply and mapcar :

CL-USER> (defun occurN (lst1 lst2)
	   (apply #'+ (mapcar #'(lambda (e) (count e lst2))
			      lst1)))
OCCURN
CL-USER> (occurn '(1 2) '(1 2 1 2 1 2))
6
CL-USER> (occurn '() '(a))
0
CL-USER> (occurn '(a) '())
0
CL-USER> (occurn '(bananas kiwis) '(kiwis pears plums bananas
cherries))
2

zion_zii skrev:

> Hi to all!
> I attempted some end of chapter exercises and got stuck with this one
> below. Please help me with HINTS so I can understand HOW TO solve it.
>
> Given two lists:
> lst1 = (bananas kiwis)
> lst2 = (kiwis pears plums bananas cherries)
>
> Write a function occurN that counts how many times an atom in lst1 also
> occurs in lst2.
> An output using the above lists would look like: (occurN lst1 lst2) is
> 2.
> This is what I have
>
> (defun occurN (lst1 lst2)
>   (cond
>     ( (or
>        (null lst1)
>        (null lst2) )
>      0)
>     (t (cond
> 	 ( (eq  (car lst1) (car lst2) )
> 	  (1+ (occurN lst1 (cdr lst2) ) ) )      ;atom may appear more than
> once
> 	 ( (occurN lst1 (cdr lst2) ) )             ;increment lst2
> 	 (t  (occurN (cdr lst1)  lst2) ) ) ) ) )
>
> My function gives me 1. I know that its only processing the first
> element in list1 and terminating when list2 is null but I dont know how
> to extend it so that it also processes the rest of the elements of
> lst1. Any hints??
From: verec
Subject: Re: recursion problem
Date: 
Message-ID: <4398d1b7$0$257$5a6aecb4@news.aaisp.net.uk>
On 2005-12-08 03:10:08 +0000, "Alejandro Guerra-Hernandez" 
<·······@uv.mx> said:

> CL-USER> (defun occurN (lst1 lst2)
> 	   (apply #'+ (mapcar #'(lambda (e) (count e lst2))
> 			      lst1)))

Just out of curiosity, why apply and not reduce?

ie:

(defun occurN (lst1 lst2)
  (reduce #'+ (mapcar #'(lambda (e) (count e lst2))
                     lst1)))
-- 
JFB  ()