Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Scheme or Racket when to use functions and when to use macros

Tags:

scheme

racket

Can someone give some general guidelines about when to use Scheme or Racket macros and when to use functions.

If you're not creating new syntax and you're not creating side effects on variables are there cases where you would either have to (or it would be more appropriate) to use macros instead of functions?

like image 505
Harry Spier Avatar asked Mar 17 '12 00:03

Harry Spier


2 Answers

Macros allow you to use completely different syntax. A macro invocation doesn't have to look like a function call at all, although the simplest macros often do. Also, macros are performed in a separate phase before runtime. So, if you need different syntax, or if you want macro expansion before runtime, then, well, use a macro.

In general, I'd say that if you can do it cleanly with a function, then use a function.

like image 172
Dan Burton Avatar answered Sep 20 '22 19:09

Dan Burton


Matthias Felleisen has a brief discussion of this question in his Racket style guide.

like image 42
Sam Tobin-Hochstadt Avatar answered Sep 22 '22 19:09

Sam Tobin-Hochstadt