From: Kim Russell
Subject: anonymous functions
Date: 
Message-ID: <1145226576.989409.247850@v46g2000cwv.googlegroups.com>
hi guys,

just a quick question. Let's say I have a list of numbers and a
variable bounded to another number. Is it possible to compare each
number of the list with the variable using anonymous functions instead
of loops, etc...also can it be done with letters too?

cheers,
kim

From: David Sletten
Subject: Re: anonymous functions
Date: 
Message-ID: <S4A0g.4305$543.3161@tornado.socal.rr.com>
Kim Russell wrote:

> hi guys,
> 
> just a quick question. Let's say I have a list of numbers and a
> variable bounded to another number. Is it possible to compare each
> number of the list with the variable using anonymous functions instead
> of loops, etc...also can it be done with letters too?
> 
> cheers,
> kim
> 

I'm not sure exactly what kind of comparison you want to do--search for 
membership? Compare by size? etc... But in any case, yes, it can be done:
(defvar *l* (list 2 4 6 8 10 12))
(defvar *x* 6)

; Which is first element of *l* that is a power of 2 and is at least as 
big as *x*?
(find-if #'(lambda (x) (and (integerp (rational (log x 2))) (>= x *x*))) 
*l*) => 8

; Does every element of *l* share same parity as *x*?
(every #'(lambda (x) (= (mod x 2) (mod *x* 2))) *l*) => T

By "letters" do you mean "characters"? If so, then again the answer is yes:
(setf *l* (coerce "wibblesnibbets" 'list))
(defvar *ch* #\i)
(substitute-if #\o #'(lambda (ch) (char= ch *ch*)) *l*) => (#\w #\o #\b 
#\b #\l #\e #\s #\n #\o #\b #\b #\e #\t #\s)

(Of course, this last example can be simplified:
(substitute #\o #\i *l*) )

Does this answer your question?

Aloha,
David Sletten
From: Kim Russell
Subject: Re: anonymous functions
Date: 
Message-ID: <1145229088.209493.238460@u72g2000cwu.googlegroups.com>
perfect.
Thanks guys.

kim
From: Pascal Bourguignon
Subject: Re: anonymous functions
Date: 
Message-ID: <87y7y5gn55.fsf@thalassa.informatimago.com>
"Kim Russell" <······@gmail.com> writes:
> just a quick question. Let's say I have a list of numbers and a
> variable bounded to another number. Is it possible to compare each
> number of the list with the variable using anonymous functions instead
> of loops, etc...also can it be done with letters too?

Your specifications are rather imprecise.  Do you mean this:

(let ((a-list-of-numbers '(1 37 42 442 1024))
      (a-variable-bounded-to-another-number 42))
   (mapcar (lambda (a-number)
             (cond ((= a-number a-variable-bounded-to-another-number) '=)
                   ((< a-number a-variable-bounded-to-another-number) '<)
                   (t '>))) a-list-of-numbers))
--> (< < = > >)

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

"Logiciels libres : nourris au code source sans farine animale."
From: Kim Russell
Subject: Re: anonymous functions
Date: 
Message-ID: <1145227686.832298.249020@z34g2000cwc.googlegroups.com>
thanks Pascal. yes, that would work.
what about if i would like to return the numbers that match, if any??
In the example, it would only return a list with 42 in it.

Thanks again,
kim
From: Eric Lavigne
Subject: Re: anonymous functions
Date: 
Message-ID: <1145228657.930635.117300@g10g2000cwb.googlegroups.com>
>thanks Pascal. yes, that would work.
>what about if i would like to return the numbers that match, if any??
>In the example, it would only return a list with 42 in it.

http://www.lisp.org/HyperSpec/Body/fun_removecm__elete-if-not.html

(remove-if-not
     #'(lambda (x)
          (equal x 42))
     '(1 37 42 442 1024))

result: (42)
From: Pascal Bourguignon
Subject: Re: anonymous functions
Date: 
Message-ID: <87psjhgj6u.fsf@thalassa.informatimago.com>
"Kim Russell" <······@gmail.com> writes:

> thanks Pascal. yes, that would work.
> what about if i would like to return the numbers that match, if any??
> In the example, it would only return a list with 42 in it.

Remove is nice. You can also have a look at mapcan:

(mapcan (lambda (x) (when (some-predicate-p x) (list (some-function-of x))))
        list)

eg:
(mapcan (lambda (x) (when (oddp x) (list (* 2 x)))) '(1 2 3 4 5 6))
(2 6 10)

If SOME-FUNCTION-OF is IDENTITY, then better use REMOVE.




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

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.