Examples of what Lisp's macros can be used for

后端 未结 8 942
自闭症患者
自闭症患者 2020-12-24 05:56

I\'ve heard that Lisp\'s macro system is very powerful. However, I find it difficult to find some practical examples of what they can be used for; things that would be diffi

8条回答
  •  醉酒成梦
    2020-12-24 06:39

    R, the standard statistics programming language, has macros (R manual, chapter 6). You can use this to implement the function lm(), which analyzes data based on a model that you specify as code.

    Here's how it works: lm(Y ~ aX + b, data) will try to find a and b parameters that best fit your data. The cool part is, you can substitute any linear equation for aX + b and it will still work. It's a brilliant feature to make statistics computation easier, and it only works so elegantly because lm() can analyze the equation it's given, which is exactly what Lisp macros do.

提交回复
热议问题