5

Doing the Y-Combinator for a single argument function such as factorial or fibonacci in Clojure is well documented: http://rosettacode.org/wiki/Y_combinator#Clojure

My question is - how do you do it for a two argument function such as this getter for example?

(Assumption here is that I want to solve this problem recursively and this non-idiomatic clojure code is there deliberately for another reason)

[non y-combinator version]

(defn get_ [n lat] (cond (empty? lat) () (= 0 (- n 1)) (first lat) true (get_ (- n 1) (rest lat)))) (get_ 3 '(a b c d e f g h i j)) 
2
  • (= 0 (- n 1)) is really an elaborate way to say (= n 1). Why are the fourth and fifth line indented more than the third, by the way? Commented Aug 14, 2010 at 12:59
  • Is there any specific reason to make this getter 1-based? Commented Aug 14, 2010 at 13:01

2 Answers 2

4

The number of args doesn't change anything since the args are apply'd. You just need to change the structure of get_:

 (defn get_ [f] (fn [n lat] (cond (empty? lat) () (= 1 n) (first lat) :else (f (dec n) (next lat))))) (defn Y [f] ((fn [x] (x x)) (fn [x] (f (fn [& args] (apply (x x) args)))))) 
 user=> ((Y getf) 3 '(a b c d e f g h i j)) c 
Sign up to request clarification or add additional context in comments.

Comments

2

It'd be pretty straight forward.

Say you've got a function H:

(def H (fn [x] (fn [x y] (stuff happens)))) 

Then you apply the same ol' Y-Combinator:

((Y H) 4 5) 

Where 4 and 5 are arguments you want to pass to H.

The combinator is essentially "dealing with" the top-level function in H, not the one that's doing the hard work (the one with arity 2, here).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.