search


Functions
Sun Mar 04 00:00:00 UTC 2018

Clojure syntax is minimal, consistent and uniform. It is one of the few languages that is homoiconic which is a fancy word meaning Clojure Code is represented in the same form as Clojure Data. This has profound implications and the main reason why Clojure, and LISP in general, distinguish itself from other programming languages. You will understand why this is profound when we cover macros. Without farther ado,

Executing functions
(f x1 x2 x3)

This is a list with 4 elements. All Clojure code is built using list. The first element of the list is f. The first element of a list is special. It is reserved for functions and other 'special forms'. We will talk about what other special forms later. The first position must be a function. Everything after the first position, x1, x2, x3 are parameters or inputs to the function f. The above syntax is how to execute a function f with parameters x1 x2 x3.

There is a built in function called +. The + function accepts zero or more parameters. Try it out yourself below. Add more numbers or remove numbers and see what happens

(+ 1 2 3)

There are also other arithmatic functions like - * /. Replace the + function with the other arithmetic functions and see what happens

Defining functions

+ - * / are built into the core language library. You can build your own functions using the macro defn. For the time being, think of macros as a function. Macros is an advance topic that we will delay for later. For our purposes, macros can be treated like any other function. The syntax to use defn to define a function is like calling any other function

(defn square [x] (* x x)) (square 3)

We defined a function named square then immediately aftewards we call the function with the value 3 which returns 9. Notice that the syntax defining a function is still in the form of a list. The first element is a function called defn. defn is not a function but for the time being let's pretend that it is. The second element is a symbol called square representing the name of the function. The [x] represents the parameters or inputs to the function. There is only one symbol in the [ ] which means the square function takes only one input named x. Experiment above with different inputs to square.

Functions always return the value of the last line evaluated. In our square function, there is only one line so the value the function returns is the value of evaulating that one line. Let's experiment with this. Modify the square function by adding another line. For example, put in a number like 1 or a string like "hi". What happened when you did this?

Like Lego Blocks

Functions are building blocks like legos. That means you can build new functions by piecing togheter existing functions like lego blocks. In the example above, we created a new function called square by reusing the built-in + function. Now that we have a square function it can be used as a building block to build other functions. Other programmers can use our function without knowing how it works internally. This is the principle of Encapsulation.

Lets create a new function to take advantage of our square function. This new function will square two numbers and add them. Lets call this function sum-of-squares

(defn sum-of-squares [x y] (+ (square x) (square y))) (sum-of-squares 2 3)

The sum-of-square function takes two parameters named x and y. It then executes the square function we defined earlier, then it calls the built-in + function on the result of squaring x and y

In Power Turtle, someone has already built functions called forward, right, left for us to comannd our turtle to move. We are simply reusing lego blocks that other people have built.

Not The End

Clojure is a small language. We've covered probably 90% of the syntax of Clojure, but the story of function does not end here. The ability to create functions and execute functions covers much of what you will do as Clojure programmers. Follow the exercise in Power Turtle to create functions of your own



Power Turtle
Mon Feb 26 00:00:00 UTC 2018

Power Turtle not only allows us to program Clojure in the browser but also has lessons in multiple languages. Try to follow the lessons in English because it is the defacto language of computer science, science/engineering/mathematics. Start your lesson by clicking on Power Turtle

Clojure is a dialect of LISP. LISP stands for LIST Processing. All Clojure programs are represented as lists which can be nested to build more complex structures. In the lesson, we saw examples like (forward 50). That is a list with two elements. The first element is a function called foward and the second element 50 is the parameter or argument of that function. What other functions did you encounter in the lesson?

Functions are at the core of functional programming. There are many different perspective of what a function is. In the examples of this lesson, functions are like commands. This definition is not correct but it is the only way you've seen how functions are used. You will build up your intuition of what a function is by coding more. Functions are like lego blocks. We can compose and build more complex things from the humble idea of a function.