Author Topic: (Challenge) Transformation of the list.  (Read 8261 times)

0 Members and 1 Guest are viewing this topic.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Transformation of the list.
« Reply #15 on: February 04, 2007, 10:18:29 AM »
:)
Evgeniy, I modify my code, now it works

Greetings Chen! :-)

At you it has perfectly turned out to perform work.
But you have decided to divide the task for two parts and have written two different programs...

I hoped to see one program, for all variants of the list...

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Transformation of the list.
« Reply #16 on: February 04, 2007, 10:22:05 AM »
You needed to unite the programs in one - then will work for all examples!  :-)

Code: [Select]
(if (atom (caar lst))
 (test-1 lst)
 (test-2 lst)
)

qjchen

  • Bull Frog
  • Posts: 285
  • Best wishes to all
Re: (Challenge) Transformation of the list.
« Reply #17 on: February 05, 2007, 03:17:00 AM »
In order to fit the two case in one, I have to use a global variable-> *A
Now it can sove the three sample.

but it make me very sad :-(, I think it must have a good solve method.
I will try to solve it in a more simple way.

Code: [Select]
(defun ab (lst1 lst2 / res)
  (foreach x lst1
    (foreach y lst2
      (if (atom y)
(setq y (list y)
      *A T
)
(if (and
      (atom (car y))
      (not *A)
    )
  (setq y (list y)
*A nil
  )
)
      )
      (setq res (append
  res
  (list (cons x y))
)
      )
    )
  )
  res
)
(defun test (lst / res)
  (if (= (cdr lst) nil)
    (setq res (car lst))
    (setq res (ab (car lst) (test (cdr lst))))
  )
)
http://qjchen.mjtd.com
My blog http://chenqj.blogspot.com (Chinese, can be translate into English)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Transformation of the list.
« Reply #18 on: February 05, 2007, 03:45:12 AM »
My congratulations Chen!
Last program works correctly! :)

edit >>
 :-(

Code: [Select]
(test '(((11)) ((21) (22)) ((31))))
return
(((11) (21) 31) ((11) (22) 31)) 
but necessary
(((11) (21) (31)) ((11) (22) (31)))
« Last Edit: February 05, 2007, 03:53:17 AM by ElpanovEvgeniy »

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: (Challenge) Transformation of the list.
« Reply #19 on: February 05, 2007, 04:54:40 AM »
Hi,

Quite the same as the one I posted, but recursive form.

Code: [Select]
(defun test (l / sub1 sub2)

  (defun sub1 (e l)
    (if l
      (cons (append e (list (car l)))
    (sub1 e (cdr l))
      )
    )
  )

  (defun sub2 (l)
    (if (cadr l)
      (sub2
(cons (apply 'append
     (mapcar '(lambda (x)
(sub1 x (cadr l))
      )
     (car l)
     )
      )
      (cddr l)
)
      )
      (car l)
    )
  )
 
  (sub2 (cons (mapcar 'list (car l)) (cdr l)))
)
Speaking English as a French Frog

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Transformation of the list.
« Reply #20 on: February 05, 2007, 05:04:34 AM »
Hi,

Quite the same as the one I posted, but recursive form.

Excellently!
You have made the program which is very similar to mine...
All differences - at me all in one function (test).

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Transformation of the list.
« Reply #21 on: February 05, 2007, 11:27:46 AM »
My version of the program... 

Code: [Select]
(defun te (l)
 ;; (te l)
 ;; By ElpanovEvgeniy
 (if (cdr l)
  (apply
   (function append)
   (mapcar (function (lambda (a) (mapcar (function (lambda (b) (cons a b))) (te (cdr l)))))
           (car l)
   ) ;_  mapcar
  ) ;_  apply
  (mapcar (function list) (car l))
 ) ;_  if
) ;_  defun

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: (Challenge) Transformation of the list.
« Reply #22 on: February 05, 2007, 12:30:55 PM »
 :-o One more time, a wonder of concision !!!
« Last Edit: February 06, 2007, 01:04:04 AM by gile »
Speaking English as a French Frog

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: (Challenge) Transformation of the list.
« Reply #23 on: February 05, 2007, 04:42:24 PM »
WOW!!!

TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

qjchen

  • Bull Frog
  • Posts: 285
  • Best wishes to all
Re: (Challenge) Transformation of the list.
« Reply #24 on: February 07, 2007, 08:05:22 PM »
so short code.

Evgeniy, could you tell me how to learn mapcar and recursive, is the only way that practice, or there are some quick way.:)
http://qjchen.mjtd.com
My blog http://chenqj.blogspot.com (Chinese, can be translate into English)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (Challenge) Transformation of the list.
« Reply #25 on: February 07, 2007, 09:10:37 PM »
so short code.

Evgeniy, could you tell me how to learn mapcar and recursive, is the only way that practice, or there are some quick way.:)

 :wink:  ... about 15 years of practice   :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.