Macros expansions
« Saturday, October 05, 2013 »

Macros Scheme

Macros is the thing that is really interesting about Scheme. Creating a macro that will transform at compile time the code to something static could possibly improve greatly performances…

Let say we have a function that takes 2 parameter, a and b. We would like to rewrite this function to the sum of both parameters. In the code, we would have a number instead of 2 variables getting summed.

(sum 10 20) would expand to 30

And the following piece of code would expand as such:

1
2
3
(let ((x (sum 23 45))
      (y (sum 30 70)))
  (+ x y))

To:

1
2
3
(let ((x 68)
      (y 100))
  (+ x y))

I'm not certain, but it might be even possible to achieve something like this

1
2
3
(let ((x (sum 23 45))
      (y (sum 30 70)))
  (sum x y))

That would expand to:

68
comments powered by Disqus

Copyright © 2015 Loïc Faure-Lacroix