FOOP recursive method

Started by refrainrence, March 17, 2014, 12:46:28 PM

Previous topic - Next topic

refrainrence

Hi,

i'm trying to build a tree of objects to represent html markup. I think of a method that will build a string in this way:

1. write the open tag to a string in (self 2) defined directly in fuction

2. run this method for each object in the list of objects in the field (self 1)

    3. append (self 2) of element in the list to the (self 2) of the object the method is operating on at the moment

4. write the close tag



i get 'list expected' error for both following samples ( second one written to simplify the problem)



(new Class 'Tag)

(context 'Tag)

(constant 'markup 1)
(constant 'opentag 0)
(constant 'closetag 1)
(constant 'inner 2)
(constant 'v 3)
(define (gen)
  (set 's1 "")
  (extend s1 (self markup opentag))
  (dolist (el (self inner))
    (:gen el)
    (extend s1 "nn" (el Tag:v)))
  (extend s1 (self markup closetag))
  (setf (self v) s1))

(context MAIN)

(set 't3 (Tag '("<div>" "</div>") '() ""))
(set 't2 (Tag '("<div style="position: absolute">" "</div>") '(t3) ""))
(set 't1 (Tag '("<div style="position: relative">" "</div>") '(t2) ""))

;(push (t2 Tag:inner) t3)
;(push (t1 Tag:inner) t2)
(:gen t1)
(println (t1 Tag:v))
(println t1)

(exit)





(new Class 'Tag)

(context Tag)
(define (run)
  ;(set 'l1 (self 1))
  ;(println (list? (self 1)) " " (self 3) " " l1)
  (dolist (el (self 1))
    (:run el)
    (println (self 2))))
(context MAIN)

(set 't1 (Tag '((Tag '() 1 "a") (Tag '() 2 "b") (Tag '() 3 "c")) 4 "d"))
(:run t1)

(exit)




I'll try to substitute dolist with map or sth else.

refrainrence

#1
I tried to simplify this case further using this code:



(new Class 'C)

(context C)

(define (run)
  (println (self 2))
  (dolist (el (self 1) (:run el))))
(context MAIN)

(set 'o1 (C '((C ((C ((C () "sdf")) "zxc")) "qwe")) "asd"))
(:run o1)

(exit)




It seems to run as expected since i removed nested ' (apostrophes or what do u call them..).

My lack of experience in lisp showed up and will get me in trouble again in the future for sure :-)

Looks like it's solved. If anything else unexpected happens i will come back.

refrainrence

#2
Both dolist and map get the job done:

(new Class 'Tag)

(context Tag)
(define (run)
  (println (self 3))
  ;(dolist (el (self 1)) (:run el)))
  (map (curry :run) (self 1)))
(context MAIN)

(set 't1 (Tag '((Tag () 1 "a") (Tag () 2 "b") (Tag () 3 "c")) 4 "d"))
(:run t1)

(exit)

refrainrence

#3
Below the corrected code mentioned first in this thread. I'll try to add a word about nested quote to the topic title. And maybe a word that it's solved. I should check what the forum rules say about it :-) If some expert could add a word to my newest explorations in the land of lisp, i would be grateful :-)

(new Class 'Tag)

(context 'Tag)

(constant 'markup 1)
(constant 'opentag 0)
(constant 'closetag 1)
(constant 'inner 2)
(constant 'v 3)
(define (gen)
  (extend (self 3) (self markup opentag))
  (dolist (el (self inner))
    (:gen el)
    (extend (self 3) "nn" (el Tag:v) "nn"))
  (extend (self 3) (self markup closetag)))

(context MAIN)

(set 't3 (Tag (list "<div>" "</div>") (list) ""))
(set 't2 (Tag (list "<div style="position: absolute">" "</div>") (list t3) ""))
(set 't1 (Tag (list "<div style="position: relative">" "</div>") (list t2) ""))

;(push (t2 Tag:inner) t3)
;(push (t1 Tag:inner) t2)
(:gen t1)
(println (t1 Tag:v))

(exit)

Astrobe

#4
Maybe a useful tip: use letex to avoid fiddling manually with self-fields. For instance:



(letex (H '(self 1) L '(self 2))
   (define (rectangle:perimeter) (* 2 (+ H L)))
   (define (rectangle:area) (* H L)))