2

Here is two functions which request the same arguments and return the same boolean type value. Such as:

 (defn Foo1 [x] (< x 3))
 (defn Foo2 [x] (> x -10))

But I am confused when I define the function below:

 (def Foo3 (or Foo1 Foo2))

Can you guys tell me how it works. Thank you very much!

1
  • (def Foo3 (some-fn Foo1 Foo2)) will also give you what you're looking for. Commented Mar 29, 2013 at 17:59

2 Answers 2

1

(defn f [a] a) is just shortcut for (def f (fn [a] a))

If your second argument to the def binding is a function, then first argument is function also.

Sign up to request clarification or add additional context in comments.

4 Comments

In my example.When I call Foo3 with an agrument which I want to get two return from Foo1 and Foo2 respectively.But in fact,Foo3 just skip Foo2 and return the value which is from Foo1
Just because or evaluates first paremeter to true, and then not needed to calculate next one, because whole result is true.
It seems not that case. I have try even Foo1 return false, the second function Foo2 is not called.And Foo3 will return whatever Foo1 has returned.
I see, you missed parameters to your helper functions. The problem that (or Foo1 Foo2) do not call functions at all. Function itself evaluates to true so your result is always true. Working example: (def Foo3 #(or (Foo1 %) (Foo2 %)))
1

Assuming you are trying to combine the conditions, you probably want:

(defn foo3 [x] (or (foo1 x) (foo2 x)))

That is, you are defining a new function foo3 whose result is the result of or-ing the results of calling foo1 and foo2 with the same parameter x.

P.S. It's conventional to name functions in lowercase in Clojure.

1 Comment

Thank you.That is what I want, but I want to know how my problem comes from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.