Author Topic: Coordinates compertion  (Read 10352 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Coordinates compertion
« Reply #30 on: July 29, 2015, 05:11:33 AM »
@ Lee: take another look at line 6 of the code in your last post:
Code: [Select]
(cons (cadr lst) (cddr lst)) :-o

Sorry roy, I don't follow?  :?
Code: [Select]
(setq lst '(0 1 2 3 4))
(cons (cadr lst) (cddr lst)) => (1 2 3 4)
(cdr lst) => (1 2 3 4)

Ah - of course! Thanks roy.

Shay Gaghe

  • Newt
  • Posts: 89
Re: Coordinates compertion
« Reply #31 on: July 29, 2015, 08:05:26 AM »
Lee and Roy...can u speak English please  :police: :police: :police:

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Coordinates compertion
« Reply #32 on: July 29, 2015, 08:29:55 AM »
Lee and Roy...can u speak English please  :police: :police: :police:
Maybe you don't know the meaning of '=>'?
'=>' means: 'The return value of the previous code is:'

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Coordinates compertion
« Reply #33 on: July 29, 2015, 08:36:04 AM »
@ Shay:
Can you explain how/why you create this list?:
Code: [Select]
(setq lst
  '(
    ("a" ((0 2 4) (0 7 0) (0 2 0)))
    ("b" ((0 2 0) (0 5 0) (0 2 0)))
    ("c" ((3 2 0) (0 2 0) (9 3 0)))
  )
)

bruno_vdh

  • Guest
Re: Coordinates compertion
« Reply #34 on: July 29, 2015, 09:17:37 AM »
Hello,
Irneb reading your excellent speech, makes me want to continue on this path
By adding arguments to functions "Reduce" and "PointMaxY"

Code: [Select]
(defun reduceKey (operation funKey lst)
  (if (cdr lst)
    (reduceKey operation funKey (cons ((eval operation) funKey (car lst) (cadr lst)) (cddr lst)))
    (car lst)
  )
)

(defun PointMaxRank (Rank pt1 pt2)
  (setq Rank (eval Rank))
  (if (> (Rank pt2) (Rank pt1))
    pt2
    pt1
  )
)

Code: [Select]
(("a" ((0 2 4) (0 7 0) (0 2 0))) ("b" ((0 2 0) (0 5 0) (0 2 0))) ("c" ((3 2 0) (0 2 0) (9 3 0))))
_$ (reduceKey 'PointMaxRank 'cadadr (mapcar '(lambda (item) (list (car item) (reduceKey 'PointMaxRank 'cadr (cadr item)))) lst))
("a" (0 7 0))

Regards,

bruno_vdh

  • Guest
Re: Coordinates compertion
« Reply #35 on: July 29, 2015, 10:46:05 AM »
An equivalent of the foreach Version proposed by Lee, with mapcar ...
Code: [Select]
(defun fun (lst / key pt)
  (mapcar '(lambda (l)
             (mapcar '(lambda (xyz) (if (< (cadr pt) (cadr xyz)) (setq pt  xyz  key (car l))))
                     (cadr l)
             )
           )
          lst
  )
  (list key pt)
)

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Coordinates compertion
« Reply #36 on: July 29, 2015, 10:53:16 AM »
Hello,
Irneb reading your excellent speech, makes me want to continue on this path
By adding arguments to functions "Reduce" and "PointMaxY"
I think that Irneb (reduce) function is intended as a generic function. Adding an argument, as in your (reduceKey), makes the function less so.

bruno_vdh

  • Guest
Re: Coordinates compertion
« Reply #37 on: July 29, 2015, 11:04:33 AM »
I think that Irneb (reduce) function is intended as a generic function. Adding an argument, as in your (reduceKey), makes the function less so.
Yes, I totally agree with that
« Last Edit: July 29, 2015, 11:48:50 AM by bruno_vdh »

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Coordinates compertion
« Reply #38 on: July 30, 2015, 04:29:04 AM »
Inspired by irneb's REDUCE:

An implementation of FOLD may also be of use. Fold belongs to the same 'family' of functions as Reduce.
(Fold is) The fundamental list iterator.

Recursive implementation:
Code - Auto/Visual Lisp: [Select]
  1. (defun fold (func val lst)
  2.   (if lst
  3.     (fold func (func (car lst) val) (cdr lst))
  4.     val
  5.   )
  6. )

Some examples taken from http://srfi.schemers.org/srfi-1/srfi-1.html#FoldUnfoldMap:
Code - Auto/Visual Lisp: [Select]
  1. ; Add up the elements of lst:
  2. (setq lst '(0 1 2 3 4))
  3. (fold + 0 lst) => 10
  4.  
  5. ; Reverse lst:
  6. (setq lst '(0 1 2 3 4))
  7. (fold cons nil lst) => (4 3 2 1 0)
  8.  
  9. ; How many symbols in lst:
  10. (setq lst '(0 a 2 b 4 c))
  11. (fold (lambda (x cnt) (if (= (type x) 'sym) (1+ cnt) cnt)) 0 lst) => 3
  12.  
  13. ; Length of the longest string in lst:
  14. (setq lst '("a" "aa" "aaa" "aaaa" "a" "aa" "aaaaaaa" "a"))
  15. (fold (lambda (str val) (max (strlen str) val)) 0 lst) => 7

The current problem:
Code - Auto/Visual Lisp: [Select]
  1. (setq lst
  2.   '(
  3.     ("a" ((0 2 4) (0 7 0) (0 2 0)))
  4.     ("b" ((0 2 0) (0 5 0) (0 2 0)))
  5.     ("c" ((3 2 0) (0 1 0) (9 3 0)))
  6.   )
  7. )
  8. (defun PointMaxY (pt1 pt2) ; Irneb.
  9.   (if (> (cadr pt2) (cadr pt1)) pt2 pt1)
  10. )
  11. (defun MainFunc (sub val / pt)
  12.   (setq pt (fold PointMaxY (caadr sub) (cdadr sub)))
  13.   (if
  14.     (or
  15.       (not val)
  16.       (> (cadr pt) (cadadr val))
  17.     )
  18.     (list (car sub) pt)
  19.     val
  20.   )
  21. )
  22. (fold MainFunc nil lst) => ("a" (0 7 0))

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Coordinates compertion
« Reply #39 on: July 30, 2015, 04:32:12 AM »
roy, I think you'd enjoy this thread: http://www.theswamp.org/index.php?topic=37362  :wink:

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Coordinates compertion
« Reply #40 on: July 30, 2015, 06:18:03 AM »
Inspired by irneb's REDUCE:
Nice!  8)


There's quite a few of these "standard" functions which ADesk omitted when they "copied" the original XLisp from the 70s. As per that link Lee shows ... trying to add them back because a later language (F#) had actually implemented them as it saw them in the 50+ year old Lisps.  ;D
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Coordinates compertion
« Reply #41 on: July 30, 2015, 10:31:53 AM »
roy, I think you'd enjoy this thread: http://www.theswamp.org/index.php?topic=37362  :wink:
Thanks for pointing that topic out. Food for thought indeed...

Shay Gaghe

  • Newt
  • Posts: 89
Re: Coordinates compertion
« Reply #42 on: August 01, 2015, 08:29:33 AM »
@ Shay:
Can you explain how/why you create this list?:
Code: [Select]
(setq lst
  '(
    ("a" ((0 2 4) (0 7 0) (0 2 0)))
    ("b" ((0 2 0) (0 5 0) (0 2 0)))
    ("c" ((3 2 0) (0 2 0) (9 3 0)))
  )
)

Hi roy

Ill try to explain what im doing,

Im from the constriction discipline, my task is to construct a supporting wall array between two planed ground levels, the one that behind the wall and the other is in front of the wall , (there is “exist” line but its off-topic now) Each ground level is represented by a line.

I distribute points along those lines and retrieve those points as a list associated with a key, by comparing their Y values I can know:

  • Which point is the highest/lowest in the wall?
    What line owns the highest/lowest point?

In order to get the answer I must use association list. In fact I need first to sort out which point is highest/lowest in which line, and which line has the highest/lowest point among all three lines.
There are some situation where the point will be rejected if its owner is “exist” , in that case the second lowest/height point will be tested.

Code: [Select]
(setq lst
   '(
        ("front" ((0 3 4) (0 4 0) (0 2 0)))
        ("back" ((0 1 3) (0 5 0) (0 3 0)))
        ("exist" ((3 2 0) (0 4 0) (9 3 0)))
    )
)

Code: [Select]
(defun foo3 (lst)

  (vl-sort
    (mapcar
      (function
(lambda (grp)

  (list (car grp)
(vl-sort (cadr grp)
(function
   (lambda (a b)
     (> (cadr a) (cadr b))

     )
   )
)
)
  )
)
      lst
      )
    (function
      (lambda (a b)
(> (cadr (caadr a)) (cadr (caadr b)))
)
      )
    )
  )

Code: [Select]
(("back" ((0 5 0) (0 3 0) (0 1 3))) ("front" ((0 4 0) (0 3 4) (0 2 0))) ("exist" ((0 4 0) (9 3 0) (3 2 0))))
how to shorten it?

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Coordinates compertion
« Reply #43 on: August 03, 2015, 04:21:26 AM »
@ Shay:
I have a hard time picturing your problem (design of retaining walls?). But that is not very important. The fact is that you are the programmer who is creating this list structure. It is not thrown at you by the CAD program. You should use a structure that works best for you and whatever algorithm you are planning. It is unlikely that there is only a single option. Maybe you don't see the alternative solutions now but when you look back on the finished program after a couple of years you no doubt will.

One thing I would do is pre-sort the point lists before creating the lst list.

Perhaps it can also make sense to work with 3 variables: ptLstFront, ptLstBack and ptLstExist.

Or perhaps the list structure below can work. As you have learnt from the contributions in this topic, it makes sorting the list much easier.
Code: [Select]
(setq lst
  '(
    ("front" (0 3 4))
    ("front" (0 4 0))
    ("front" (0 2 0))
    ("back"  (0 1 3))
    ("back"  (0 5 0))
    ("back"  (0 3 0))
    ("exist" (3 2 0))
    ("exist" (0 4 0))
    ("exist" (9 3 0))
  )
)