[Cs254f11] Recursion without loop

Lee Spector lspector at hampshire.edu
Wed Nov 2 23:33:58 EDT 2011


That's fine, but do know for the future that you can always give a function two bodies, one of which takes fewer arguments and which just calls the one with more arguments, giving default values for the additional arguments. Here's an example of this from clojinc:

(defn assume-cheese
  "calls with one arg use a default of cheese for second"
  ([main-ingredient]
    (assume-cheese main-ingredient 'cheese))
  ([main-ingredient second-ingredient]
    (concat '(a) (list main-ingredient) '(and) (list second-ingredient) '(sandwich with a pickle))))

; #'clojinc.core/assume-cheese

(assume-cheese 'ham)

; (a ham and cheese sandwich with a pickle)

On Nov 2, 2011, at 11:05 PM, Maxwell William Fair Levit wrote:

> Thank you both so much, I'm going to use Lee's code for now, primarily because
> it takes only two arguments and the third argument in Tom's solution (as well
> as my own attempt) is ungainly.
> 
> -Max

--
Lee Spector, Professor of Computer Science
Cognitive Science, Hampshire College
893 West Street, Amherst, MA 01002-3359
lspector at hampshire.edu, http://hampshire.edu/lspector/
Phone: 413-559-5352, Fax: 413-559-5438



More information about the Cs254f11 mailing list