Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple arguments to mapcar

I'm sure this is a very beginner question in lisp, as I am just learning the language.

I have a function in clisp called count. It counts the number of times a given atom appears in a list. What I'd like to do is be able to call count multiple times with different parameters, but the same list to search.

For example, I'd like to count the number of 'A, 'B, and 'C in the list, hypothetically. I was hoping I could do something like this:

(mapcar 'count '(A B C) myList)

I've figured out that this doesn't work because each of the elements in '(A B C) are being paired up with only one of the elements in myList. What is the appropriate idiomatic way to apply a function with an additional input parameter to each item in a list?

To further clarify, I'd like to be able to take '(A B C) and '(A A B C C C) as input and produce (2 1 3).

like image 270
Alex Pritchard Avatar asked May 11 '11 05:05

Alex Pritchard


1 Answers

To call the function count repeatedly with each item from a list (A B C), every time counting matching items the same sequence mylist:

(mapcar (lambda (x) (count x mylist)) '(A B C))
like image 172
Terje Norderhaug Avatar answered Oct 24 '22 17:10

Terje Norderhaug