Learning Clojure - Part 2 | Writing a Basic Function

in #clojure3 years ago (edited)

Learning Clojure.jpg
The time has come to finally start writing some Clojure code.

I figured that a great start would be to try to solve a few Project Euler problems.

If you are not familiar with Project Euler, their website describes it like this:

Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.

For instance, the first problem, the one I'm set to solve, goes like this:

Problem 1
Multiples of 3 and 5
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.
I did this basic implementation in JavaScript to show one possible solution. Had I spent a little more time, I could probably come up with a neat one-liner as well.
let sum = 0
for (let i = 0; i < 1000; i++) {
  if (i % 3 == 0 || i % 5 == 0) {
    sum += i
  }
}
console.log(sum) // => 233168

It's pretty simple. For all numbers from 0 until 1000 we add the number to an accumulator if it's divisible by 5 or 3.

I'll admit that I'm not quite sure where to start when it comes to implementing a similar solution in Clojure. I think I will start off by getting familiar with functions. From math, you might be familiar with functions on the form f(x) = x2. Most programming languages which I am used to, follow a similar structure, where a function can be called by writing the function name followed by a set of parentheses containing the function's input variables. In Clojure, however, you write both the function name and the parameters inside the same pair of parentheses. In JavaScript, if you had a sum function, you could call it like this:

sum(2, 4) // => 6

In Clojure, however, you can sum numbers like this:
(+ 2  4) // => 6

You might notice that this is quite similar to Polish notation. in case you are not familiar with Polish notation, Wikipedia explains it like this:
Polish notation (PN), also known as normal Polish notation (NPN), Łukasiewicz notation, Warsaw notation, Polish prefix notation or simply prefix notation, is a mathematical notation in which operators precede their operands, in contrast to reverse Polish notation (RPN) in which operators follow their operands. It does not need any parentheses as long as each operator has a fixed number of operands. The description "Polish" refers to the nationality of logician Jan Łukasiewicz, who invented Polish notation in 1924.

Also note that there are no commas separating the input parameters.

To solve the first Project Euler problem, I would, among other things, have to be able to determine whether a number was divisible by three, so I wrote the following function:

(defn is-divisible-by-three [number] (= 0 ( mod number 3 )))

Note that when defining the function, the function name is preceded by the keyword defn, not unlike the keyword def in Python. Actually, defn is a combination of def and fn. fn defines the function and def binds it to its name. If I wanted, I could write my function like this:
(def is-divisible-by-three (fn [number] (= 0 (mod number 3))))

Moreover, when defining a function, the input parameters are surrounded by square brackets. If there were more than one input parameter, the additional parameters would be inside the same pair of square brackets, separated by whitespaces.

Now that I've made a basic function, it might be in my interest to run it. I googled around and it seemed like nobody had a simple solution for running Clojure code. Eventually I just googled online Clojure compiler and found repl.it which provides a Clojure Compiler, REPL and IDE. I've used repl.it before, back when I made my first neural network from scratch in Python. Back then, repl.it didn't support that many languages, but in the year that has passed since then, it has eveolved to support nearly all programming languages and a number of popular frameworks.

is-divisible-by-three.jpg

After writing my function in the editor window, I simply press the Run button and then I can test my function by writing ( is-divisible-by-three 18 ) in the REPL console:
is-divisible-by-three test.jpg

This confirms that 18 is divisible by 3.

Now you might wonder what the point of making a function which checks whether a number is divisible by 3 is. After all, it's just as easy to write ( = 0 ( mod 18 3 )) as it is to write ( is-divisible-by-three 18 ). Well, I did manage to make a function, didn't I?

I totally forgot that I was supposed to solve that Project Euler Problem. I guess that'll have to wait for another post.

Coin Marketplace

STEEM 0.76
TRX 0.09
JST 0.072
BTC 54525.21
ETH 4099.56
BNB 597.08
SBD 7.03