Author Topic: My head hurts.. problem with a list  (Read 7072 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: My head hurts.. problem with a list
« Reply #15 on: March 01, 2011, 02:16:47 PM »
Another:

Code: [Select]
(defun _sum ( l )
  (if (cadr l)
    (cons (car l) (_sum (cons (+ (car l) (cadr l)) (cddr l))))
    (list (car l))
  )
)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: My head hurts.. problem with a list
« Reply #16 on: March 01, 2011, 05:08:16 PM »
Another, using gc:scan as defined here.

EDIT: rewrited gc:scan function so that the first argument is a 'quoted' function (as others 'high order' AutoLISP functions)
Code: [Select]
(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))

F#
Code: [Select]
List.scan (+) lst.Head lst.Tail
« Last Edit: March 01, 2011, 05:45:41 PM by gile »
Speaking English as a French Frog

Brick_top

  • Guest
Re: My head hurts.. problem with a list
« Reply #17 on: March 02, 2011, 04:28:18 AM »
Thanks a lot for all the options!

I have a lot to study

Brick_top

  • Guest
Re: My head hurts.. problem with a list
« Reply #18 on: March 02, 2011, 07:04:30 AM »
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)

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


ronjonp

  • Needs a day job
  • Posts: 7529
Re: My head hurts.. problem with a list
« Reply #19 on: March 02, 2011, 10:11:26 AM »
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)

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


:-) Good deal ... if you have any questions don't hesitate to ask.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: My head hurts.. problem with a list
« Reply #20 on: March 02, 2011, 12:09:05 PM »
a shortened Ron's
Code: [Select]
(mapcar '+ (cons 0 lst) lst)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: My head hurts.. problem with a list
« Reply #21 on: March 02, 2011, 12:10:52 PM »


EDIT:  Wait a minute... I thought the result was meant to be as in post #5?
« Last Edit: March 02, 2011, 12:26:24 PM by Lee Mac »

ronjonp

  • Needs a day job
  • Posts: 7529
Re: My head hurts.. problem with a list
« Reply #22 on: March 02, 2011, 12:29:09 PM »
a shortened Ron's
Code: [Select]
(mapcar '+ (cons 0 lst) lst)

Knew you couldn't resist  :-P   .very nice BTW  8-)
« Last Edit: March 02, 2011, 12:37:07 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Brick_top

  • Guest
Re: My head hurts.. problem with a list
« Reply #23 on: March 02, 2011, 01:48:17 PM »
a shortened Ron's
Code: [Select]
(mapcar '+ (cons 0 lst) lst)

Is this for real?  :-o   :-o

Don't have autocad right now

Brick_top

  • Guest
Re: My head hurts.. problem with a list
« Reply #24 on: March 02, 2011, 01:55:08 PM »


EDIT:  Wait a minute... I thought the result was meant to be as in post #5?

yeah, it is as in post #5

btw, thanks for your solution but it is way over my head as of now as is gile's
« Last Edit: March 02, 2011, 02:04:39 PM by Brick_top »

ronjonp

  • Needs a day job
  • Posts: 7529
Re: My head hurts.. problem with a list
« Reply #25 on: March 02, 2011, 02:20:03 PM »
a shortened Ron's
Code: [Select]
(mapcar '+ (cons 0 lst) lst)

Is this for real?  :-o   :-o

Don't have autocad right now

That gives the same result as my "wrong" answer ... but nonetheless cool.  :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: My head hurts.. problem with a list
« Reply #26 on: March 02, 2011, 02:23:25 PM »
EDIT:  Wait a minute... I thought the result was meant to be as in post #5?

yeah, it is as in post #5

Nope...

Code: [Select]
_$ (mapcar '+ (cons 0 '(10 20 30 40 50)) '(10 20 30 40 50))
(10 30 50 70 90)

From your description in post#5, correct result should be:

Code: [Select]
(10 30 60 100 150)

Guitar_Jones

  • Guest
Re: My head hurts.. problem with a list
« Reply #27 on: March 02, 2011, 02:55:49 PM »
Another
Code: [Select]
(defun _sum (l)(mapcar '(lambda (x) (apply '+ (member x (reverse l)))) l))

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: My head hurts.. problem with a list
« Reply #28 on: March 02, 2011, 03:12:23 PM »
Another
Code: [Select]
(defun _sum (l)(mapcar '(lambda (x) (apply '+ (member x (reverse l)))) l))

Nice idea, but be careful for duplicate elements!  :lol:

Guitar_Jones

  • Guest
Re: My head hurts.. problem with a list
« Reply #29 on: March 02, 2011, 03:16:44 PM »
Thanks Lee. :ugly: