0 Members and 1 Guest are viewing this topic.
(defun _sum ( l ) (if (cadr l) (cons (car l) (_sum (cons (+ (car l) (cadr l)) (cddr l)))) (list (car l)) ))
(defun gc:scan (fun acc lst) (cons acc (mapcar (function (lambda (x) (setq acc ((eval fun) acc x)))) lst)))(gc:scan '+ (car lst) (cdr lst))
List.scan (+) lst.Head lst.Tail
Here's one way to do it:Code: [Select](setq lst '(10.2757 12.1581 33.2103 45.11 1.1 2.2 3.3))(cons (car lst) (mapcar (function (lambda (l1 l2) (+ l1 l2))) lst (cdr lst)));;returns;;(10.2757 22.4338 45.3684 78.3203 46.21 3.3 5.5)
(setq lst '(10.2757 12.1581 33.2103 45.11 1.1 2.2 3.3))(cons (car lst) (mapcar (function (lambda (l1 l2) (+ l1 l2))) lst (cdr lst)));;returns;;(10.2757 22.4338 45.3684 78.3203 46.21 3.3 5.5)
Quote from: ronjonp on March 01, 2011, 11:39:05 AMHere's one way to do it:Code: [Select](setq lst '(10.2757 12.1581 33.2103 45.11 1.1 2.2 3.3))(cons (car lst) (mapcar (function (lambda (l1 l2) (+ l1 l2))) lst (cdr lst)));;returns;;(10.2757 22.4338 45.3684 78.3203 46.21 3.3 5.5) Hi, I just want to thank you for that last part "lst (cdr lst)" didn't know I could do that and it is helping me a lot in other problems I was having. I'm trying to understand what you can do with lambda and mapcar
(mapcar '+ (cons 0 lst) lst)
a shortened Ron'sCode: [Select](mapcar '+ (cons 0 lst) lst)
EDIT: Wait a minute... I thought the result was meant to be as in post #5?
Quote from: VovKa on March 02, 2011, 12:09:05 PMa shortened Ron'sCode: [Select](mapcar '+ (cons 0 lst) lst)Is this for real? Don't have autocad right now
Quote from: Lee Mac on March 02, 2011, 12:10:52 PMEDIT: Wait a minute... I thought the result was meant to be as in post #5?yeah, it is as in post #5
_$ (mapcar '+ (cons 0 '(10 20 30 40 50)) '(10 20 30 40 50))(10 30 50 70 90)
(10 30 60 100 150)
(defun _sum (l)(mapcar '(lambda (x) (apply '+ (member x (reverse l)))) l))
AnotherCode: [Select](defun _sum (l)(mapcar '(lambda (x) (apply '+ (member x (reverse l)))) l))