Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7831687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:50:33+00:00 2026-06-02T11:50:33+00:00

I’m working on a Clojure algorithm to solve the problem posed here: http://spin.atomicobject.com/2011/05/31/use-clojure-to-move-drugs-a-programming-challenge/ and

  • 0

I’m working on a Clojure algorithm to solve the problem posed here: http://spin.atomicobject.com/2011/05/31/use-clojure-to-move-drugs-a-programming-challenge/ and I’ve run into a hiccup.

I’m using a recursive algorithm (perhaps not the right choice to begin with), to walk over a vector of “doll” structs that are ordered by highest to lowest value-to-weight ratio. The relevant code is:

(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))
    )
  )
)

That code is working correctly; returnable-dolls contains the desired vector of doll structs to return as a solution. Unfortunately, when I attempt to return the returnable-dolls vector to the calling position, I’m receiving the following error:

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

Line 82-83 read:

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

I’m stumped as to what might be causing the issue in the compiler error, and since Clojure seems to prefer terse error messages over stack traces (or at least, the REPL functionality in Clooj does), I’m at a loss to how to fix it. If anyone has any suggestions, I’d greatly appreciate them!

Thanks in advance.

EDIT:

I’ve modified the code with the suggested fixes by those in the answers and the comments and provided a handful of comments to help illustrate the flow control that’s going on. Hopefully, by illustrating my thinking, someone will be able to give me an idea where I’m going wrong.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:50:36+00:00Added an answer on June 2, 2026 at 11:50 am

    The following code incorporates most of the suggestions you already received in other answers and others, namely:

    • 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

    I can’t test it without having the definition of the ancillary functions you have (e.g. will-fit?), but at least should fix a couple of issues (and bring the code in a more readable form):

    (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))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.