Jump to content

Positive list values


Small Fish

Recommended Posts

Hi how do I convert all numbers in my list to positive values?

My guess is that I need to use 'abs' and 'apply' but not sure what combo.

Now I'm all googled out looking for a way to do it.

Anyone know please? ... thanks

 

 

for example:

 

 

(setq lista '((-20.4 -10.0)(20.0 -50.0)(58.0 -60.6)))

 

convert to:

 

(setq newlista '((20.4 10.0)(20.0 50.0)(58.0 60.6)))

Link to comment
Share on other sites

if you use something like this, it doesn't matter what your list looks like. you could run it on a list with or without embedded lists.

ie: (list -2 2 (list 3 -3 "B" "E" (list 3 -3 -2 "G"))) will become (2 2 (3 3 "B" "E" (3 3 2 "G")))

;;;Convert all values in list and sublists to positive numbers
;;;#List - list with values to convert
;;;Alan J. Thompson, 06.14.09
(defun AT:AbsList (#List)
 (mapcar
   '(lambda (x)
      (cond
        ((vl-consp x) (AT:AbsList x))
        ((member (type x) (list 'INT 'REAL)) (abs x))
        (T x)
      ) ;_ cond
    ) ;_ lambda
   #List
 ) ;_ mapcar
) ;_ defun

Link to comment
Share on other sites

Nice recursion Alan... You've been spending too long at the Swamp.. :P

haha, i didn't even realize what i'd done until i was finished.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...