English 中文(简体)
4. 要求改观的服装回馈价值
原标题:Clojure returning values from recursive call

http://spin.atomicobject.com/05/31/use-clojure-to-move-drugs-a-programming-challenge/“rel="nofollow” http://spin.atobject.com/05/31/use-clojure-to-move-drugs-a-programming-challenge/ 和我跑进了一步。

采用复读算法(也许不是从正确选择开始)的Im,用最高至最低的数值对加权比率订购的“doll”构件。 相关法典是:

(defn get-carryable-dolls 
  [dolls carryable-dolls]
  (def doll (first dolls)) ;initializing for use in multiple places
  (def rest-dolls (rest dolls)) ;initializing for use in multiple places
  (
    if (will-fit? doll (get-weight-sum carryable-dolls)) 
    ( ;will fit
      (
        if 
        (= carryable-dolls {})
        (def new-doll-set [doll]) ;First trip, get rid of empty set by initializing new
        (def new-doll-set (flatten [doll carryable-dolls])) ;otherwise, flatten set into vector of structs
      )
      ;tests to see if we have any more dolls to test, and if so, recurses. Otherwise, should pass the resultant vector
      ;up the stack. it appears to be the "else" potion of this if statement that is giving me problems.
      (if (not= () rest-dolls) (get-carryable-dolls rest-dolls new-doll-set) (vec new-dolls))
    )
    ( ;will not fit
      ;gets the rest of the dolls, and sends them on without modifying the vector of structs
      ;it appears to be the "else" potion of this if statement that is giving me problems.
      (if (not= () rest-dolls) (get-carryable-dolls rest-dolls carryable-dolls) (vec carryable-dolls))
    )
  )
)

该法典正在正确运作;可回收的载体含有希望的重压载体,以作为解决办法回归。 不幸的是,当我试图将可回收的病媒退回到电话站时,我会发现以下错误:

CompilerException clojure.lang.ArityException: Wrong number of args (0) passed to: PersistentVector,compiling:(drugmover	ests.clj:83)

第82-83段改为:

(def empty-dolls {})
(def designated-dolls (get-carryable-dolls sorted-values empty-dolls))

我对编辑错误中可能产生这一问题的推论感到困惑,由于Clojure似乎偏好对 st痕(或至少是Cloj的REPL功能)的透差错电,因此,我对如何加以纠正感到损失。 如果有人提出任何建议,我非常赞赏这些建议。

提前感谢。

<>光线>

我对守则进行了修改,提出了答复和评论中建议的办法,并提供了一些意见,以帮助说明正在进行的流动控制。 我希望,通过说明我的想法,有人能够给我一个我错了的想法。

最佳回答

下面的法典纳入了你在其他答复和其他答复中已经收到的大部分建议,即:

  • getting rid of the superflous (and wrong) parenthesis
  • formatting the code in a more idiomatic way
  • use loop/let instead of def for local name bindings
  • use seq for checking empty lists
  • remove the unnecessary check for an empty carryable-dolls seq before adding an element

我可以在没有界定你所具备的辅助职能的情况下对其进行测试(例如:will-fit?),但至少应当确定几个问题(并使该守则更具可读性):

(defn get-carryable-dolls 
  [dolls carryable-dolls]
  (loop [doll (first dolls)
         rest-dolls (rest dolls)]
    (if (will-fit? doll (get-weight-sum carryable-dolls))
      (let [new-doll-set (if (seq carryable-dolls)
                           (cons doll carryable-dolls)
                           [doll])]
        (if (seq rest-dolls)
          (recur rest-dolls new-doll-set)
          (vec new-dolls)))
      (if (seq rest-dolls)
        (recur rest-dolls carryable-dolls)
        (vec carryable-dolls)))))

The following is a complete refactoring of the code that leverages the standard reduce function and defines a function that provides the core decision logic whether a doll has to be included or not in the result:

(defn add-if-fits [dolls doll]
  (if (will-fit? doll (get-weighted-sum dolls))
    (cons doll carryable-dolls)
    carryable-dolls))

(defn get-carryable-dolls [dolls carryable-dolls]
  (reduce add-if-fits carryable-dolls dolls))
问题回答

这部法典中有许多教区,它们造成了问题。 我强烈建议你以其他每个人的方式制定你的法典,这样就会很容易地发现这种错误。 我甚至可以猜测你试图做些什么,因此,我可以改写整个座标,但需要注意的有关事情是:<条码>的星号:

(if test then else)

No extra parens are allowed around any of these things: (if true 1 2) is fine, for example, but (if (true) 1 2) would try to call true as a function, and fail because it s a boolean. If you want to "group" expressions together and evaluate them for side effects, you want (do expr1 expr2), not (expr1 expr2).

页: 1 代码,是造成这一错误的原因:

如下文法将产生与外派相同的错误(如要求其他部分制造病媒,则与你相同:

((vec {:a 10 :b 100}))

在REP执行这一规定时,你会看到同样的例外:

java.lang.IllegalArgumentException: Wrong number of args (0) passed to: PersistentVector (NO_SOURCE_FILE:0)




相关问题
Recursive same-table query in SQL Server 2008

I have the following table in a SQL Server 2008 database: Id Name ParentFolder -- ---- ------------ 1 Europe NULL 2 Asia NULL 3 Germany 1 4 UK 1 5 China ...

Finding a class within list

I have a class (Node) which has a property of SubNodes which is a List of the Node class I have a list of Nodes (of which each Node may or may not have a list of SubNodes within itself) I need to be ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签