Unit 3. Conditionals and Functions

 

After completing this Unit, you will know how to write functions that have some simple conditional logic like “if this condition holds, then this, otherwise that” or more complex logic like: “in case a this, in case b that, in case c etc...”.

To appreciate the possibilities opened by the conditional logic, even before knowing how to create functions, we need a way to make our code depends on something. Otherwise, it will be a bit boring to write conditional code that always evaluates to the same value!

You will learn how to create variables whose values can be modified. You will also get a taste about how fun it feels to manipulate variables in a dynamically typed language.

But obviously the best way to write code that can be parametrized is to write functions which are the basis of any functional programming language.

We will discuss the following topics:

  • Lesson 11 explains how to name stuff in Clojure
  • Lesson 12 explores how to write conditional logic with if and cond.
  • Lesson 13 explores how to create functions

Starting from this Unit, all the code snippets presented in the book are meant to be run in the Klipse REPL that we have presented in Unit 2. Before moving forward, please make sure that you have installed the Klipse REPL on your machine and that you feel comfortable with evaluating Clojure expressions inside the Klipse REPL.