Clojure: What exactly is tail position for recur?

前端 未结 2 694
走了就别回头了
走了就别回头了 2020-12-13 19:35

Whats the precise definition of \"tail position\" for recur in clojure. I would think that it would be the last item in a loop S-expression, but in the example below it seem

2条回答
  •  盖世英雄少女心
    2020-12-13 19:43

    Just to supplement the excellent answer from Paul above, The Joy of Clojure (ed1) provides a table (Table 7.1) that shows exactly where tail position is in various forms/expressions, which I've reproduced below. Look for where the word "tail" occurs in each form/expression:

    |---------------------+-------------------------------------------+---------------|
    | Form                | Tail Position                             | recur target? |
    |---------------------+-------------------------------------------+---------------|
    | fn, defn            | (fn [args] expressions tail)              | Yes           |
    | loop                | (loop [bindings] expressions tail)        | Yes           |
    | let, letfn, binding | (let [bindings] expressions tail)         | No            |
    | do                  | (do expressions tail)                     | No            |
    | if, if-not          | (if test then tail else tail)             | No            |
    | when, when-not      | (when test expressions tail)              | No            |
    | cond                | (cond test test tail ... :else else tail) | No            |
    | or, and             | (or test test ... tail)                   | No            |
    | case                | (case const const tail ... default tail)  | No            |
    |---------------------+-------------------------------------------+---------------|
    

提交回复
热议问题