FOOP recursive method

Q&A's, tips, howto's

FOOP recursive method

Postby refrainrence » Mon Mar 17, 2014 7:46 pm

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)

Code: Select all
(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 "\n\n" (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)




Code: Select all
(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
 
Posts: 10
Joined: Thu Aug 08, 2013 10:03 pm

Re: FOOP recursive method

Postby refrainrence » Mon Mar 17, 2014 10:07 pm

I tried to simplify this case further using this code:

Code: Select all
(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
 
Posts: 10
Joined: Thu Aug 08, 2013 10:03 pm

Re: FOOP recursive method

Postby refrainrence » Mon Mar 17, 2014 10:16 pm

Both dolist and map get the job done:
Code: Select all
(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
 
Posts: 10
Joined: Thu Aug 08, 2013 10:03 pm

Re: FOOP recursive method

Postby refrainrence » Mon Mar 17, 2014 10:59 pm

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 :-)
Code: Select all
(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) "\n\n" (el Tag:v) "\n\n"))
  (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)
refrainrence
 
Posts: 10
Joined: Thu Aug 08, 2013 10:03 pm

Re: FOOP recursive method

Postby Astrobe » Tue Mar 18, 2014 10:17 am

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

Code: Select all
(letex (H '(self 1) L '(self 2))
   (define (rectangle:perimeter) (* 2 (+ H L)))
   (define (rectangle:area) (* H L)))
Astrobe
 
Posts: 39
Joined: Mon Jan 11, 2010 9:41 pm


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 0 guests

cron