Substitution

Started by Kazimir Majorinc, June 08, 2009, 04:36:01 AM

Previous topic - Next topic

Kazimir Majorinc

I need function that performs substitution with syntax:


; (serial-substitute '(-> (A B) (B (A B)) (-> B (A C) (B C)))
;                     ((A abc) (B cde) (C xxx)))
;                    
; =>
;
;  (-> (abc cde) (cde (abc cde)) (-> cde (abc xxx) (cde xxx)))


I wrote six such functions, and this is the fastest one:


(set 'serial-substitute6
     (lambda(F s)
        (eval (let((vars (map first s)))
                  (list 'local
                        vars
                        '(bind s)
                        (append '(expand F)
                                 (map quote vars)))))))

(set 'formula '(-> (A B) (B (A B)) (-> B (A C) (B C))))
(set 'substitution ' ((A abc) (B cde) (C xxx)))
(println (serial-substitute6 formula substitution))

(exit)


Did I missed something in Newlisp that can make it even faster?
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

Lutz

#1
The 'exapnd' function can already do this in one statement, because it can take a binding list:




(expand '(-> (A B) (B (A B)) (-> B (A C) (B C))) '((A abc) (B cde) (C xxx)))

=> (-> (abc cde) (cde (abc cde)) (-> cde (abc xxx) (cde xxx)))


see: http://www.newlisp.org/newlisp_manual.html#expand">http://www.newlisp.org/newlisp_manual.html#expand

Kazimir Majorinc

#2
Excellent. It is about four times faster than my solution.
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.