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
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
"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."
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
>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)
"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.