if-else branching in clojure

if else clojure
clojure if multiple statements
if-let clojure
clojure if-let multiple bindings
if do clojure
clojure let
clojure map
clojure case

I'm teaching myself Clojure.

In a non-FP language, I could easily enough write nested if's, and if I didn't specifically put an else, then control would just flow out of the if block. For example:

Thing myfunc()
{
  if(cond1)
  {
    if(cond2)
      return something;
  }
  return somethingelse;
}

However, in Clojure, there's no return statement (that I know of), so if I write:

(defn myfunc []
  (if (cond1)
      (if (cond2) something))
  somethingelse)

then there's no "return" on "something". It seems to just kind of say, ok, here we have a value, now let's keep on executing. The obvious solution would be to combine the conditions, i.e.:

(if (and (cond1) (cond2))
    something
    somethingelse)

but this gets unwieldy/ugly for large conditions. Also, it would take additional finagling to add a statement to the "else" part of cond1. Is there any kind of elegant solution to this?


This is the subtle difference between imperative and functional approach. With imperative, you can place return in any place of the function, while with functional the best way is to have clear and explicit exeecution paths. Some people (me including) prefer the latter approach in imperative programming as well, recognizing it as more obvious and manageable and less error-prone.

To make this function explicit:

Thing myfunc() {
  if(cond1) {
    if(cond2)
      return something;
  }

  return somethingelse;
}

You can refactor it to:

Thing myfunc() {
  if(cond1 && cond2) {
      return something;
  } else {
    return somethingelse;
  }
}

In Clojure, its equivalent is:

(defn myfunc []
  (if (and cond1 cond2) 
      something
      somethingelse))

If you need an "else", your Java version could become:

Thing myfunc() {
  if(cond1) {
    if(cond2) {
      return something;
    } else {
      return newelse;
    }
  } else {
    return somethingelse;
  }
}

... and its Clojure equivalent:

(defn myfunc []
  (if cond1
      (if cond2 something newelse)
      somethingelse))

Learn Clojure - Flow Control, Since the if-branching uses simple conditionals, it is frequently used to divide into two states: true or false. In Clojure, if is a special form. The syntax is: (if test� Clojure if-else Statement. If-else statements are common in programing languages. Clojure does not have an explicit if-else statement. Instead, Clojure's if statement provides a place for an 'else' case. A Clojure if statement would look like this (if true "that is true" "that is false") This statement looks at the first true.


Imperative Languages have if-statements that say if this then do that else do that and functional languages have if-expressions that say if this return that else return this. it's a different way of looking at the same idea, that reflects a very different approach to expressing problems. in Functional languages everything has a value, really everything, even if you don't do anything with that value.

When i was making the transition it helped a lot to ask my self "what result should this function return" instead of the question "what should this function do" that i was accustomed to asking.

Conditional if, Be aware that the only two values considered "logical false" in Clojure ;; are nil at least the following ;; conditional statements in Clojure: if, cond, when, if-let,� The ‘if-do’ expression in Clojure is used to allow multiple expressions to be executed for each branch of the ‘if’ statement. We have seen in the classic ‘if’ statement in Clojure that you can just have two statements, one which is executed for the true part and the other which is for the false part. But the ‘if-do’ expression allows you to use multiple expressions.


(if (and (cond1) (cond2))
     something
     somethingelse)

(cond 
    (and (cond1) (cond2)) something
    :else somethingelse)

cond does do this if you want to compare the same thing; in a switch-case you can use condp.

I don't see that kind of code very often, but that's the way to do it.

if - clojure.core, See examples for "if" explaining Clojure's idea of logical true ;; and logical false. link if-let multiple bindings version ;; Edited: Else branch did not work with� In Clojure, the condition is an expression which evaluates it to be either true or false. If the condition is true, then statement#1 will be executed, else statement#2 will be executed. The general working of this statement is that first a condition is evaluated in the ‘if’ statement. If the condition is true, it then executes the statements.


There is no explicit return statement in Clojure, but your code will "return" on "something" because you don't have any expressions after that if and in Clojure the result of the last expression is used as the function’s return value.

if-let - clojure.core, If a test returns logical true, cond evaluates and returns the value of the (defn print-cond [xx] (cond (< xx 6) "less than 6" (< xx 8) "less than 8" :else "Greater than � In an imperative language, control flow is achieved via special constructs, e.g. if-else statements. In functional languages, control flow mechanisms are either functions or at least function- like in that they return values. For instance, if in Clojure executes one of two branches and returns the value returned by that branch.


You could also use the (cond) macro:

(defn length-checker [a b]
  (cond
   (= 3 (count (str a))) (if (= 3 (count (str b)))
                   (println "both numbers are 3 digits long")
                   (println "first number is 3 digits, but the 2nd not!"))
   :else (println "first- or both of the numbers are not 3 digits")))

cond - clojure.core, Or even this, because the “else” branch of if defaults to nil : (if (condition? ) (then -expression )) There's an argument to be made for any� Long running switch cases or if-else-if constructs are avoided in OOP using polymorphism wherever it is applicable. instead of branching by matching a value, branching is done at class-level itself. How can similar approach be applied in Functional Programming paradigm, Clojure specifically ?


Clojure Don'ts: Single-branch if – Digital Digressions by Stuart Sierra, The 'if-do' expression in Clojure is used to allow multiple expressions to be executed for each branch of the 'if' statement. We have seen in the classic 'if'� © Rich Hickey. All rights reserved. Eclipse Public License 1.0. Brought to you by Zachary Kim.. Tweet


Clojure - If/do Expression, Whatever your preferred language is, you are probably used to if and switch statements. In Clojure, there is an if form that is similar to an if statement and a cond� Flow control operators are also extensible via macros, which allow the compiler to be extended by user code. We won’t be discussing macros today, but you can read more about them at Macros, Clojure from the Ground Up, or Clojure for the Brave and True, among many other places.


Lesson 12. Conditional branching with if and cond, Notice that if uses operand position to associate operands with the then and else branches: the first operand is the then branch, and the second operand is the� clojure.core Available since 1.0 (cond & clauses) Takes a set of test/expr pairs. It evaluates each test one at a time. If a test returns logical true, cond evaluates