Skip to content

Instantly share code, notes, and snippets.

@mfandl
Last active August 1, 2018 21:23
Show Gist options
  • Save mfandl/8e049999cb8165fa75ee6938adcafd0d to your computer and use it in GitHub Desktop.
Save mfandl/8e049999cb8165fa75ee6938adcafd0d to your computer and use it in GitHub Desktop.
clojure school lesson 1 recap and exercises
;; WIP material
;; RECAP
; clojure expressions are lists
; syntactically, lists are values in parentheses delimited by white space
(1 2 3)
; clojure expects each expression is built in such a way,
; that the first element is a function name followed by its arguments
; (we will soon start calling these things correctly)
(function-name arg1 arg2 arg3)
; for example
(+ 1 2)
; => 3
; clojure expressions can be nested
(+ 1 (- 4 2))
; => 3
; you can define your own function with defn
(defn function-name [param1 param2]
('the 'function 'body 'is 'a 'list))
; example:
(defn my-square [a]
(* a a))
; my-square returns the square of the provided argument
; and can be called as any built in function
(my-square 4)
; => 16
; functions can be composed of multiple expressions. the return value of the
; function is the return value of the last expression
(defn my-happy-square [a]
(println "yay")
(* a a))
; or to reuse what we already have
(defn my-happy-square [a]
(println "yay")
(my-square a))
; we can define function to support multiple arities
(defn my-increment
([a b] (+ a b))
([a] (my-increment a 1)))
(my-increment 5)
; => 6
(my-increment 9 10)
; => 19
;; data
; numbers
5 ; integers
5.2 ; floats
; characters
\c ; this represents the letter 'c'
; strings
"hello there"
; collections
(1 2 3) ; this is a list which we already know
[1 2 3] ; and this is a vector, which we'll learn more about soon.
; we also already know it from function definitions
{:height 182 :weight 155} ; this is a map, it associates keys with values
; if this particular map describes a person, (s)he might
; need to put down a little
;; some useful functions, macros and special forms
; Basic mathematical operations
; addition
(+ 1 2 3 4 5 6)
; => 21
; subtraction
(- 1 2 3 4 5 6)
; => -19
; division
(/ 2 3)
; => 2/3
; multiplication
(* 2 3 4)
; => 24
; equality
(= 1 2)
; => false
(not= 1 2)
; => true
(> 2 3)
; => false
(< 2 3)
; => true
; try also <= and >=
; applying a collection of values to a function
(apply + [1 2 3 4 5 6])
; => 21
; collection related functions
; constructiong collections programatically
(vector 1 2 3)
; => [1 2 3]
(list 1 2 3)
; => (1 2 3)
(hash-map :height 182 :weight 31)
; => {:weight 31, :height 182} - the colon only enhances readability. you
; can use it, but you do not have to
; adding to the collection - conj
(conj [1 2 3] 4)
; => [1 2 3 4]
(conj (list 1 2 3) 4)
; => (4 1 2 3)
;with maps, conj expects a vector with key-value pair
(conj {:width 5} [:legth 2])
; => {:width 5, :length 2}
; conj adds to the beginning of the list, but to the end of the vector.
; this is because of the nature of the collection types and the goal of
; conj to be fast. for each collection type, conj adds new elements in such
; a way, that the traversal of the collection is not necessary
; determining collection length
(count [1 1 1 1 1])
; => 5 - works same way with lists
; to insert multiple elements, use 'into'
(into [1 2 3] [4 5])
; => [1 2 3 4 5]
;; EXERCISES
; 0) write a function that computes average of values in a collection
; example call: (my-average [1 2 3 4 5]) should return 3
; expect the input array to have at least one element
; 1) how would you represent a 2D triangle in clojure? can you think of multiple
; representations? write a function that creates a triangle in your representation.
; 2) write a function that determines, whether your triangle is righ-angled
; you will likely want to define some helper functions too
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment