[Clojure] – My first step. Understanding the basics of the syntax

Below are a lot of example for the first steps. For understand the syntax and basics concepts. I’m using Intellij IDEA to develop. If you prefere you can use your terminal, online tools or your favorite text editor.

How to define the file, directory and author ?

(ns
  ^{:author camilamacedo}
  mypkg.test)

Screen Shot 2016-06-21 at 8.56.07 PM

How to do a function ?

(defn loose [] (print "You loose"))

How to call a function?

(defn loose [] (print "You loose"))

(loose)

Console:

Screen Shot 2016-06-21 at 8.48.33 PM

How to do one if ? How to do a loop?

(ns
  ^{:author camilamacedo}
  mypkg.test)

(defn end [] (print "End"))

(defn myloop [num]
  (if (= num 0)
    (end)
    (do
      (print num)
      (myloop (- num 1))
      )
    )
  )

(myloop 5)

Console:

Screen Shot 2016-06-21 at 8.58.03 PM

We also can change the line :

 (myloop (- num 1))

For

 (myloop (dec num))

the result will be the same!

How to define a variable ?

(def qty 5)

If I want use this in the example above, so we will have :

(defn end [] (print "End"))

(def qty 5)

(defn myloop [num]
  (if (= num 0)
    (end)
    (do
      (print num)
      (myloop (- num 1))
      )
    )
  )

(myloop qty)

And the same result will show in the console.

How to compare values ?

Use only one “=”. Look :

if x = 10 :

(defn comp [x] (print (= x 10)))
(comp 5 )
(println \n)
(comp 10 )

Screen Shot 2016-06-21 at 9.07.11 PM

if x is not = 10 : ( Use not= )

(defn comp [x] (print (not= x 10)))
(comp 5 )
(println \n)
(comp 10 )

Screen Shot 2016-06-21 at 9.09.05 PM

How to call a function by other function ?

Problem for an example:

;#Problem
;fib(0) = 0
;fib(1) = 1
;fib(n) = fib(n-1) + fib(n-2), if n >= 2

;#Solution

(defn fib[x]
  (println "Start with x =" x)
  (if (= x 0) 0
              (if (= x 1) 1
                          (+ (fib (- x 1)) (fib (- x 2)))))
  )

(fib 2)

Console:

Screen Shot 2016-06-21 at 9.14.54 PM

How to comment the code ?

Use “;” before the code . Example

Screen Shot 2016-06-21 at 9.20.06 PM

How to work with a set of values?

  • How to define ?
(def set #{"L" "M" "A"})
(println set)

Console : #{L M A}

  • How to know if this has a specific value ?
(println (contains? set "L") )

Console : true

(println (contains? set "T") )

Console : false

  • How to add a value ?
(println (conj set "X") )

Console: #{L M X A}

  • How to remove a value ?
(println (disj set "M") )

Console: #{L A}

How to work with arrays?

(def nums [1 2 3 4 4])
(println nums )

Console : [1 2 3 4 4]

What the diference between arrays and sets ?

Basically, in the array we can has  duplicate values.

Example :

  • Look a set with duplicate values :
(def set #{"L" "M" "A" "A"})
(println set)

java.lang.IllegalArgumentException: Duplicate key: A,
  • Look a array with duplicate values :
(def nums ["L" "M" "A" "A"])
(println nums )

Console: [L M A A]

How to work with a map of values?

If we using a map we can apply a function for which value in the map. Look:

  • We define an array of numbers:
(def nums [1 2 3 4 4])
  • We define a function that multiplies a number by 2:
(defn mult [x] (* x 2))
  • We call the map function that takes the previous applied to the numbers list:
(println (map mult nums))
  • Console:

Screen Shot 2016-06-21 at 10.08.23 PM

Screen Shot 2016-06-21 at 10.06.15 PM.png

How to calculate the remainder of a division?

Use the function : ( rem divisor dividend )

Examples :

(println (rem 4 2) )
Console : 0
(println (rem 5 2) )
Console : 1
; Remove all odd numbers array
(def nums [1 2 3 4 4])
(println (remove (fn [x] (= (rem x 2) 1)) nums) )
Console: (2 4 4)

How to work with filter function ?

This a example, using filter to remove of the array all numbers that are more or equals than 2 and less or equals than 4.

Screen Shot 2016-06-21 at 10.18.30 PM Console:

Screen Shot 2016-06-21 at 10.20.09 PM

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s