newLISP Fan Club

Forum => newLISP newS => Topic started by: Kazimir Majorinc on June 08, 2009, 04:36:01 AM

Title: Substitution
Post by: Kazimir Majorinc on June 08, 2009, 04:36:01 AM
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?
Title:
Post by: Lutz on June 08, 2009, 06:06:11 AM
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
Title:
Post by: Kazimir Majorinc on June 08, 2009, 08:08:41 AM
Excellent. It is about four times faster than my solution.