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?
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
Excellent. It is about four times faster than my solution.