Page 1 of 1

Substitution

Posted: Mon Jun 08, 2009 11:36 am
by Kazimir Majorinc
I need function that performs substitution with syntax:

Code: Select all

; (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:

Code: Select all

(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?

Posted: Mon Jun 08, 2009 1:06 pm
by Lutz
The 'exapnd' function can already do this in one statement, because it can take a binding list:

Code: Select all

(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

Posted: Mon Jun 08, 2009 3:08 pm
by Kazimir Majorinc
Excellent. It is about four times faster than my solution.