Exploding a list using patterns

Started by jopython, November 03, 2012, 02:39:59 PM

Previous topic - Next topic

jopython

Is there a Lisp pattern where I can explode a list which has BEGIN and END markers


("exb" "BEGIN" "unknown" "num" "of" "items" "END" "BEGIN" "ex3" "END" "something" "and" "so" "on" )

To


(("BEGIN" "unknown" "num" "of" "items" "END") ("BEGIN" "ex3" "END"))

The builtin explode function doesnt allow custom functions  where we can

accomplish this.

winger

#1

(setf lst  '("exb" "BEGIN" "unknown" "num" "of" "items" "END" "BEGIN" "ex3" "END" "something" "and" "so" "on" ))
(setf ix (map (fn (x) (first x))  (ref-all {BEGIN|END} lst regex)))
(push 0 ix)
(apply (fn (, y z) (println (push ( y  (inc (- z y)) lst) result -1))) ix 3)

;result
(("BEGIN" "unknown" "num" "of" "items" "END") ("BEGIN" "ex3" "END"))




or


(dolist (e lst)
   (cond ((= e "END") (and tmplst (push tmplst result -1) (setf bid nil tmplst nil)))
         ((true? bid) (push e tmplst -1))
         ((= e "BEGIN") (and (setf bid true) (push e tmplst -1))))
 )

;result
(("BEGIN" "unknown" "num" "of" "items") ("BEGIN" "ex3"))


May be we need a match-all function :)
Welcome to a newlisper home:)

http://www.cngrayhat.org\">//http://www.cngrayhat.org

winger

#2
Quote from: "winger"
(setf lst  '("exb" "BEGIN" "unknown" "num" "of" "items" "END" "BEGIN" "ex3" "END" "something" "and" "so" "on" ))
(setf ix (map (fn (x) (first x))  (ref-all {BEGIN|END} lst regex)))
(push 0 ix)
(apply (fn (, y z) (println (push ( y  (inc (- z y)) lst) result -1))) ix 3)

;result
(("BEGIN" "unknown" "num" "of" "items" "END") ("BEGIN" "ex3" "END"))


or


(dolist (e lst)
   (cond ((= e "END") (and tmplst (push tmplst result -1) (setf bid nil tmplst nil)))
         ((true? bid) (push e tmplst -1))
         ((= e "BEGIN") (and (setf bid true) (push e tmplst -1))))
 )

;result
(("BEGIN" "unknown" "num" "of" "items") ("BEGIN" "ex3"))


May be we need a match-all function :)
Welcome to a newlisper home:)

http://www.cngrayhat.org\">//http://www.cngrayhat.org

jopython

#3
Thank you winger for your timely responses.



It is interesting to see how newLisp allows you to use symbols (tmplst and result) without 'declaring' ahead.

I am not used to that kind of thinking. Is that considered good practice?

Lutz

#4
using match for the same task:


(setf lst
  '("exb" "BEGIN" "unknown" "num" "of" "items" "END" "BEGIN" "ex3" "END" "something" "and" "so" " on" ))

(while (set 'm (match '(* "BEGIN" * "END" *) lst))
    (println "===>" (m 1))
    (set 'lst (last m)))

;===>("unknown" "num" "of" "items")
;===>("ex3")

jopython

#5
Thanks Lutz for that terse, and at the same time intuitive code.

I wish match also accepts regular expressions. Something like this.



(match '(* {BEGIN} * "END" *) lst)