Author Topic: iteration code : short and efficiency challenge  (Read 13710 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: iteration code : short and efficiency challenge
« Reply #30 on: July 20, 2015, 12:55:29 AM »
Quote
i cant test it right now but looking at the code cant really  understand why you put s in a list?

What does the function return ... which variable ??



what is the result of this
Code - Auto/Visual Lisp: [Select]
  1. (setq startpoint '(1 1 1) )

Then:
Code - Auto/Visual Lisp: [Select]
  1. (setq result (list startpoint))

Then :
Code - Auto/Visual Lisp: [Select]
  1. (setq result (cons '(2 2 2) result))
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: iteration code : short and efficiency challenge
« Reply #31 on: July 20, 2015, 01:16:03 AM »
< ..>

no i dont , but i read about it, i assume its this

Code: [Select]
(defun isCoordReal (crd)
  (car
    (mapcar
      (function
(lambda (x y z)
  (= (type x) 'REAL)
  (= (type y) 'REAL)
  (= (type z) 'REAL)
  )
)
      (list (car crd))
      (list (cadr crd))
      (list (caddr crd))
      )
    )
  )


i cant understand why but the returned value is a list (T)  rather than an element. the car exp is totally ignored.

Perhaps have a play with something like this as an alternative :
Code - Auto/Visual Lisp: [Select]
  1. (defun point-p (pt )
  2.   (and (listp pt) (= 3 (vl-list-length pt)) (vl-every 'numberp pt))
  3.  
  4. )

edit kdub: revised from (vl-member-if 'numberp pt)to(vl-every 'numberp pt)
« Last Edit: July 20, 2015, 05:43:32 AM by Kerry »
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.

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #32 on: July 20, 2015, 04:30:07 AM »
Quote
i cant test it right now but looking at the code cant really  understand why you put s in a list?

What does the function return ... which variable ??



what is the result of this
Code - Auto/Visual Lisp: [Select]
  1. (setq startpoint '(1 1 1) )

Then:
Code - Auto/Visual Lisp: [Select]
  1. (setq result (list startpoint))

Then :
Code - Auto/Visual Lisp: [Select]
  1. (setq result (cons '(2 2 2) result))

I get it now
Thank you Kerry

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #33 on: July 20, 2015, 04:44:43 AM »
< ..>

no i dont , but i read about it, i assume its this

Code: [Select]
(defun isCoordReal (crd)
  (car
    (mapcar
      (function
(lambda (x y z)
  (= (type x) 'REAL)
  (= (type y) 'REAL)
  (= (type z) 'REAL)
  )
)
      (list (car crd))
      (list (cadr crd))
      (list (caddr crd))
      )
    )
  )


i cant understand why but the returned value is a list (T)  rather than an element. the car exp is totally ignored.

Perhaps have a play with something like this as an alternative :
Code - Auto/Visual Lisp: [Select]
  1. (defun point-p (pt )
  2.   (and (listp pt) (= 3 (vl-list-length pt)) (vl-member-if 'numberp pt))
  3. )

i didnt know about numbrtp function.  :embarrassed: Thanx

anyhow i would be happy to understand why my writen function return a list and not an atom as it were instructed to do.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: iteration code : short and efficiency challenge
« Reply #34 on: July 20, 2015, 04:47:05 AM »
Perhaps have a play with something like this as an alternative :
Code - Auto/Visual Lisp: [Select]
  1. (defun point-p (pt )
  2.   (and (listp pt) (= 3 (vl-list-length pt)) (vl-member-if 'numberp pt))
  3. )
Not doubt you meant to use vl-every instead of vl-member-if:
Code: [Select]
(point-p '(1 "a" 3)) => T

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: iteration code : short and efficiency challenge
« Reply #35 on: July 20, 2015, 05:00:27 AM »
anyhow i would be happy to understand why my writen function return a list and not an atom as it were instructed to do.
But your function does not return a list! It is flawed though:
Code: [Select]
(isCoordReal '(1.0 "a" 3.0)) => T

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: iteration code : short and efficiency challenge
« Reply #36 on: July 20, 2015, 05:45:00 AM »
Perhaps have a play with something like this as an alternative :
Code - Auto/Visual Lisp: [Select]
  1. (defun point-p (pt )
  2.   (and (listp pt) (= 3 (vl-list-length pt)) (vl-member-if 'numberp pt))
  3. )
Not doubt you meant to use vl-every instead of vl-member-if:
Code: [Select]
(point-p '(1 "a" 3)) => T


Thanks Roy ,

I'm rushing too much ..   :embarrassed:

Code revised.
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.

bruno_vdh

  • Guest
Re: iteration code : short and efficiency challenge
« Reply #37 on: July 20, 2015, 06:04:05 AM »
Hi,

wow....its too advanced for me. can you please give a simple code which explain better how to design recursive functions?

Perhaps this version it will be easier for you ...
Code: [Select]
(defun f (s i n)
  (if (> n 0)
    (cons s (f (mapcar '+ s i) i (1- n)))
  )
)
Or
Code: [Select]
(defun f (s i n)
  (if (> n 1)
    (cons s (f (mapcar '+ s i) i (1- n)))
    (list s)
  )
)
Code: [Select]
_$ (f '(1 2 42) '(5 0 0) 5)
((1 2 42) (6 2 42) (11 2 42) (16 2 42) (21 2 42))

One more not keep the starting point in the outcome.
Code: [Select]
(defun f (s i n)
  (if (> n 0)
    (cons (setq s (mapcar '+ s i)) (f s i (1- n)))
  )
)
Code: [Select]
_$ (f '(1 2 42) '(5 0 0) 4)
((6 2 42) (11 2 42) (16 2 42) (21 2 42))


no i dont , but i read about it, i assume its this
Code: [Select]
(defun isCoordReal (crd)
  (car
    (mapcar
      (function
(lambda (x y z)
  (= (type x) 'REAL)
  (= (type y) 'REAL)
  (= (type z) 'REAL)
  )
)
      (list (car crd))
      (list (cadr crd))
      (list (caddr crd))
      )
    )
  )

i cant understand why but the returned value is a list (T)  rather than an element. the car exp is totally ignored.

My variant:
Code: [Select]
(defun 3Dpoint-p (pt)
  (and (numberp (car pt)) (numberp (cadr pt)) (numberp (caddr pt)) (null (cdddr pt)))
)

Best Regards
« Last Edit: July 20, 2015, 06:23:55 AM by bruno_vdh »

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #38 on: July 20, 2015, 11:34:52 PM »
This should offer some minor performance improvements:

Code - Auto/Visual Lisp: [Select]
  1. (defun measure1 ( s e i / a r )
  2.     (setq r (list s)
  3.           a (angle s e)
  4.     )
  5.     (repeat (fix (/ (distance s e) i))
  6.         (setq r (cons (polar (car r) a i) r))
  7.     )
  8. )

Quote
A genius is a person who displays exceptionally superior intellectual ability, creativity, or originality, typically to a degree that is associated with the achievement of new advances in a domain of knowledge. A scholar in many subjects or a scholar in a single subject may be referred to as a genius.[1] There is no scientifically precise definition of genius, and the question of whether the notion itself has any real meaning has long been a subject of debate, although psychologists are converging on a definition that emphasizes creativity and eminent achievement.

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #39 on: July 22, 2015, 04:33:44 AM »
hi

i need mapcar to iterate each 2 items rather than each item. i sign it with this code

Code: [Select]
(defun mapcar-x2(lst)
  (mapcar
    (function
      (lambda (a b)
(command "._line" a b)
(princ (strcat "Segment length: " (distance a b) ))
      )
     )
    lst ;_ how to shuffle the list in a way a and b will have not the same number each iteration?
   lst
    )
  )

hope i am unmderstood
Thanks
Shay

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: iteration code : short and efficiency challenge
« Reply #40 on: July 22, 2015, 06:09:04 AM »
use (cdr lst) as a second argument

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #41 on: July 22, 2015, 06:31:14 AM »
im not sure i get what you say :embarrassed:

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: iteration code : short and efficiency challenge
« Reply #42 on: July 22, 2015, 07:10:06 AM »
Code: [Select]
(mapcar (function (lambda (a b)
    .....
  )
)
lst
(cdr lst)
)

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #43 on: July 22, 2015, 08:13:02 AM »
Code: [Select]
(defun mapcar-x2(lst)
  (mapcar
    (function
      (lambda (a b)

(princ (strcat "Segment length: " (distance a b) ))
      )
     )
    lst
    (cdr lst)
    )
  )


Code: [Select]
_$ (mapcar-x2 '( (0 0 0)(0 0 0)(0 0 0)(0 0 0)) )
Code: [Select]
; error: bad argument type: stringp 0.0

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: iteration code : short and efficiency challenge
« Reply #44 on: July 22, 2015, 08:13:48 AM »
Hi Evgeniy,

Nice bit of code.

I think   (f (list 1 2 42) '(5 2 0) 4)

should return  ( (21 10 42) (16 8 42) (11 6 42) (6 4 42) )

Regards,

Thanks!
I understood, but not immediately...   :-)