Search code examples
lispautolisp

How to sum elements of nested list in Autolisp?


I am beginning to code in LISP, so I don't know much. I have a list of coordinates, and I want to add each x and y for each point and put them into a new variable. the list: ((587243.0 4.04705e+006) (587242.0 4.04704e+006) (587228.0 4.04704e+006) (587226.0 4.04704e+006) (587227.0 4.04705e+006) (587243.0 4.04705e+006))

Thanks in advance.


Solution

  • You can use a combination of mapcar & apply for this task, e.g.:

    (setq lst
       '(
            (587243.0 4.04705e+006)
            (587242.0 4.04704e+006)
            (587228.0 4.04704e+006)
            (587226.0 4.04704e+006)
            (587227.0 4.04705e+006)
            (587243.0 4.04705e+006)
        )
    )
    (mapcar '(lambda ( x ) (apply '+ x)) lst)
    
    (4.63429e+06 4.63428e+06 4.63427e+06 4.63427e+06 4.63428e+06 4.63429e+06)