Clojure 递归日志 Table
Clojure Recursive Log Table
(ns logtable)
(defn displayLogTable[start stop step]
(if (> start stop) nil
(if < star stop) log10 start)
(displayLogTable ( + start step) stop step)
)
))
(defn -main []
(println "\n Enter your start, stop and step: ")
(let
[ start stop step (Integer/parseInt (read-line))]
(print (displayLogTable start stop step)
)
)
我遇到 "Too many arguments to if" 错误
我正在尝试实现一个递归函数来打印我的日志 table.
这部分代码有多个错误:
(defn displayLogTable[start stop step]
(if (> start stop) nil
(if < star stop) log10 start)
(displayLogTable ( + start step) stop step)
)
))
格式化以使其显而易见:
(defn displayLogTable[start stop step]
(if (> start stop)
nil ; 1
(if < ;2
star
stop)
log10 ; 3
start) ; 4
(displayLogTable (+ start step) stop step))
)) ; to much closing parens
if
的形式太多 (1-4),其中只允许三个 (if predicate then else)
。 2
处的 if 格式正确,但肯定不是您想要的(如果 <
为真(总是)则 star
(打字错误,很可能是 start
)else stop
).
如果我们按照 暗示的方式更正您的代码,我们会得到类似于 ...
(defn displayLogTable [start stop step]
(if (> start stop)
nil
(if (< start stop)
(Math/log10 start)
(displayLogTable (+ start step) stop step))))
这没有多大意义。我们可以将其简化为...
(defn displayLogTable [start stop step]
(loop [start start]
(cond
(> start stop) nil
(< start stop) (Math/log10 start)
:else (recur ( + start step)))))
同样徒劳。看起来您想要 start
、stop
和 step
定义的数字范围的对数序列。在 Clojure 中执行此操作的一种方法是......
(defn displayLogTable [start stop step]
(map
(fn [x] (Math/log10 x))
(range start stop step)))
例如,...
=> (displayLogTable 1 10 1)
(0.0 0.3010299956639812 0.47712125471966244 0.6020599913279624 0.6989700043360189
0.7781512503836436 0.8450980400142568 0.9030899869919435 0.9542425094393249)
注意 range
包含起点 (0
) 但不包含终点 (10
)。
Clojure处理sequences的手段(map
和range
出现在这里)往往是解决此类问题的关键。
(ns logtable)
(defn displayLogTable[start stop step]
(if (> start stop) nil
(if < star stop) log10 start)
(displayLogTable ( + start step) stop step)
)
))
(defn -main []
(println "\n Enter your start, stop and step: ")
(let
[ start stop step (Integer/parseInt (read-line))]
(print (displayLogTable start stop step)
)
)
我遇到 "Too many arguments to if" 错误 我正在尝试实现一个递归函数来打印我的日志 table.
这部分代码有多个错误:
(defn displayLogTable[start stop step]
(if (> start stop) nil
(if < star stop) log10 start)
(displayLogTable ( + start step) stop step)
)
))
格式化以使其显而易见:
(defn displayLogTable[start stop step]
(if (> start stop)
nil ; 1
(if < ;2
star
stop)
log10 ; 3
start) ; 4
(displayLogTable (+ start step) stop step))
)) ; to much closing parens
if
的形式太多 (1-4),其中只允许三个 (if predicate then else)
。 2
处的 if 格式正确,但肯定不是您想要的(如果 <
为真(总是)则 star
(打字错误,很可能是 start
)else stop
).
如果我们按照
(defn displayLogTable [start stop step]
(if (> start stop)
nil
(if (< start stop)
(Math/log10 start)
(displayLogTable (+ start step) stop step))))
这没有多大意义。我们可以将其简化为...
(defn displayLogTable [start stop step]
(loop [start start]
(cond
(> start stop) nil
(< start stop) (Math/log10 start)
:else (recur ( + start step)))))
同样徒劳。看起来您想要 start
、stop
和 step
定义的数字范围的对数序列。在 Clojure 中执行此操作的一种方法是......
(defn displayLogTable [start stop step]
(map
(fn [x] (Math/log10 x))
(range start stop step)))
例如,...
=> (displayLogTable 1 10 1)
(0.0 0.3010299956639812 0.47712125471966244 0.6020599913279624 0.6989700043360189
0.7781512503836436 0.8450980400142568 0.9030899869919435 0.9542425094393249)
注意 range
包含起点 (0
) 但不包含终点 (10
)。
Clojure处理sequences的手段(map
和range
出现在这里)往往是解决此类问题的关键。