Small Fish Posted June 11, 2009 Share Posted June 11, 2009 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))) Quote Link to comment Share on other sites More sharing options...
ronjonp Posted June 11, 2009 Share Posted June 11, 2009 This should do what you want (mapcar (function (lambda (n) (mapcar 'abs n))) lista) Quote Link to comment Share on other sites More sharing options...
Small Fish Posted June 11, 2009 Author Share Posted June 11, 2009 thanks that does the trick. Quote Link to comment Share on other sites More sharing options...
ronjonp Posted June 13, 2009 Share Posted June 13, 2009 thanks that does the trick. Glad to help Quote Link to comment Share on other sites More sharing options...
alanjt Posted June 14, 2009 Share Posted June 14, 2009 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 Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted June 14, 2009 Share Posted June 14, 2009 Nice recursion Alan... You've been spending too long at the Swamp.. Quote Link to comment Share on other sites More sharing options...
alanjt Posted June 15, 2009 Share Posted June 15, 2009 Nice recursion Alan... You've been spending too long at the Swamp.. haha, i didn't even realize what i'd done until i was finished. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.