What would be an example of an anaphoric conditional in Lisp?

☆樱花仙子☆ 提交于 2019-12-10 13:22:53

问题


What would be an example of an anaphoric conditional in Lisp? Please explain the code as well.


回答1:


An example is the Common Lisp LOOP:

(loop for item in list
      when (general-predicate item)
      collect it)

The variable IT has the value of the test expression. This is a feature of the ANSI Common Lisp LOOP facility.

Example:

(loop for s in '("sin" "Sin" "SIN")
      when (find-symbol s)
      collect it)

returns

 (SIN)

because only "SIN" is a name for an existing symbol, here the symbol SIN. In Common Lisp symbol names have internally uppercase names by default.




回答2:


Paul Graham's On Lisp has a chapter on Anaphoric Macros.

Essentially, it's a shorthand way of writing statements that avoids repeating code. For example, compare:

(let ((result (big-long-calculation)))
  (if result
      (foo result)))

and

(if (big-long-calculation)
    (foo it))

where it is a special name that refers to whatever was just calculated in (big-long-calculation).



来源:https://stackoverflow.com/questions/3920193/what-would-be-an-example-of-an-anaphoric-conditional-in-lisp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!