Author Topic: Truth serum (not a question. Just a neat find.)  (Read 1862 times)

0 Members and 1 Guest are viewing this topic.

daron

  • Guest
Truth serum (not a question. Just a neat find.)
« on: June 29, 2005, 03:25:19 PM »
I am writing a procedure that contains a decent sized cond statement. What would make it larger than it needs to be, would be if I had to add to each condition an "OR" statement and place each size within each condition, like so:
Code: [Select]
(cond ((= (or (beamsize "813") (= beamsize "815")(etc.)) udsize)(info here)). SO..... I went in search of a way to group these sizes into lists and use mapcar to give me T/nil values. That works, so I figured member ought to be able to return any true values or nil if nil. I got that working and found myself with a list (T). This won't work with cond like this: (cond ((T)()), then I remembered car will give me the first element of a list and since there should only be one element when member's done, I tried it and got the desired results. Here's the partial code:
Code: [Select]
(car (member 'T (mapcar '(lambda (x) (= pocket x)) sizelist)))
pocket is a user-supplied argument and sizelist is a list of beam sizes that can be placed into the same size pocket. Ex.
(setq sizelist (list "813" "815" "818")). Mapcar compares pocket with sizelist and returns T or nil.

Pretty cool, eh?

Jürg Menzi

  • Swamp Rat
  • Posts: 599
  • Oberegg, Switzerland
Truth serum (not a question. Just a neat find.)
« Reply #1 on: June 30, 2005, 09:26:51 AM »
How about:
Code: [Select]
(vl-position pocket sizelist)
Cheers
A computer's human touch is its unscrupulousness!
MENZI ENGINEERING GmbH
Current A2k16... A2k24 - Start R2.18

daron

  • Guest
Truth serum (not a question. Just a neat find.)
« Reply #2 on: June 30, 2005, 10:12:59 AM »
That's very nice. Things may be changing here, and I don't know to what extent, so I've the feeling that I shouldn't use any vl-code for the time being. Good info anyway.

SMadsen

  • Guest
Truth serum (not a question. Just a neat find.)
« Reply #3 on: June 30, 2005, 11:37:42 AM »
Seems you could use APPLY

(apply 'OR (mapcar '(lambda (x) (= pocket x)) sizelist))

daron

  • Guest
Truth serum (not a question. Just a neat find.)
« Reply #4 on: June 30, 2005, 11:49:29 AM »
Ah. now that is cool. Thanks.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Truth serum (not a question. Just a neat find.)
« Reply #5 on: June 30, 2005, 11:49:31 AM »
Looks like this would get the job done.

Code: [Select]
(if ((setq size (car (member pocket beamlist)))
   ;; do your thing
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.