Substitution

Notices and updates
Locked
Kazimir Majorinc
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia
Contact:

Substitution

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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Kazimir Majorinc
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia
Contact:

Post by Kazimir Majorinc »

Excellent. It is about four times faster than my solution.

Locked