全部版块 我的主页
论坛 数据科学与人工智能 人工智能
663 1
2019-10-06
LispLanguage includes an extremely powerful (TuringComplete) macro facility. Via macros LispLanguage allows the user to adapt the language to their problem domain and in its ultimate expression, turn LispLanguage into a DomainSpecificLanguage (or LittleLanguage) customized for their task.
This page is for discussing how and when to use macros and for examples of macro use, see DoWeWantLispMacros for whether or not they should be used.
Uses of Macros
Macros are/should-be used for these reasons:
  • to deal with new data sub-languages (hide quote and quasiquote)
  • to introduce new binding operators
  • to specify control among the subexpressions of a form.
(From: http://www.cs.utah.edu/plt/mailarch/plt-scheme-2001/msg00292.html)
## this is only a limited view on macros. See my comment on macros and CLOS later on this page.
Macro Systems
There are three main macro systems in use:
Examples
Since I know that I use macros for deep, meaningful stuff, here are "prototypes" for the ones I consider interesting:
  (def-synonym-function synonym original)  (def-pipe-function function-name (input-stream output-stream &rest other-args) &body body)  (def-enumerated-value-domain control &rest data)  (let-gensyms symbols &body body)  This one is a very common idiom, except it's usually called something else.  (struct-comparison-predicate struct-name reducer-name &rest fields)  I need to rework the interface to this one.  (successive-character-predicate-parser input (&body predicates) &body body)  And even more so for this one.Three of these are convenience utilities; one is an "inconvenience utility" which I don't actually use anymore. One concisely defines a whole set of functions and symbols which relate to each other, in a manner which I very often want. The remaining two are nano-languages to simplify the writing of certain sorts of code. Most of these, I use quite often. I could expound the above at great length, but I expect it's not necessary to answer your question. -- DanielKnapp
Other common macros are:
  (when-bind var condition &body body)bind var to the result of condition and evaluate body if the condition returned true
  (with-collectors (collectors &body)COLLECTORS is a list of symbols, for each element of COLLECTORS a new function is introduced in BODY, these functions collect their arguments. WITH-COLLECTORS returns all of the collected arguments as multiple values. ie:
  (with-collectors (a b)
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2022-10-30 09:47:49
点个赞加点人气
感谢分享
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群