Upload
jarppe
View
40
Download
3
Tags:
Embed Size (px)
Citation preview
ClojureBridge Helsinki17.05.2015
Tervetuloa
@SolitaOy @metosin
Agenda
10:00
13:00
15:00
17:00
Welcome Our goals First steps in programming Lunch Programming in real life More programming Break Even more programming We're done
Agenda
Our goals
(+ 3 4)(max 8 17 2)(eat "sandwich")
;; more food code(eat "cookie") ; nom nom nom(eat "donut") ; mmm donuts
(+ 1 1) ;=> 1 + 1 = 2(- 12 4) ;=> 12 - 4 = 8(* 13 2) ;=> 13 * 2 = 26(/ 27 9) ;=> 27 / 9 = 3
Infix:
Prefix:
1 + 2 * 3 / 4 + 5 - 6 * 7 / 8 + 9
(+ (- (+ (+ 1 (/ (* 2 3) 4)) 5) (/ (* 6 7) 8)) 9)
Infix:
Prefix:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9
(+ 1 2 3 4 5 6 7 8 9)
Infix:
Prefix:
1 + 2 * 3 / 4 + 5 - 6 * 7 / 8 + 9
(+ (- (+ (+ 1 (/ (* 2 3) 4)) 5) (/ (* 6 7) 8)) 9)
Open terminal
Open LightTable
File -> Open folder -> clojurebridge
(+ 4/3 7/8) ;=> 53/24(- 9 4.2 1/2) ;=> 4.3(/ 27/2 1.5) ;=> 9.0
;; strings"Salut tout le monde""Prost!";; keywords:surname:birth-date:r2;; booleanstruefalse
(def mangoes 3)(def oranges 5)(+ mangoes oranges) ;=> 8
(def fruit (+ mangoes oranges))(def average-fruit-amount (/ fruit 2))average-fruit-amount ;=> 4
Excercise• Define a symbol with value of your height
in centimetres
• Define another symbol that holds your height in inches
• Ask height of your neighbours and calculate the average of all heights
One inch is 2.54 centimetres
Average: sum of values divided by number of values
Data-structures
["a" "b" "c" "d" "e" "f"]["a" "b" "c" "d" "e" "f"] [1 2 3 4]["Answer" 42][][[1 2 3] ["a" "b" "c"]]
(count [5 10 15]) ;=> 3(nth [5 10 15] 1) ;=> 10(first [5 10 15]) ;=> 5(rest [5 10 15]) ;=> (10 15)(conj [5 10] 15) ;=> [5 10 15]
Excercise
• Define a vector containing the temperatures of your home town for next 5 days
• Use nth function to check the temperature in Tuesday
For example: http://ilmatieteenlaitos.fi
Functions
(defn reduced-price [price] (* price 0.75))
(reduced-price 120) ;=> 90(reduced-price 10) ;=> 7.5(reduced-price 0) ;=> 0.0
(defn reduced-price [price] (* price 0.75))
(reduced-price 120) ;=> 90(reduced-price 10) ;=> 7.5(reduced-price 0) ;=> 0.0
(defn reduced-price [price reduce-by] (* price reduce-by))(reduced-price 120 0.75) ;=> 90.0(reduced-price 120 0.50) ;=> 60.0
(defn reduced-price [price reduce-by] (* price reduce-by))(reduced-price 120 0.75) ;=> 90.0(reduced-price 120 0.50) ;=> 60.0
(defn reduced-price [price reduce-pct] (* price (- 1.0 (/ reduce-pct 100.0))))(reduced-price 120 25) ;=> 90.0(reduced-price 120 50) ;=> 60.0
(defn reduced-price [price reduce-by] (* price reduce-by))(reduced-price 120 0.75) ;=> 90.0(reduced-price 120 0.50) ;=> 60.0
(defn reduced-price [price reduce-pct] (* price (- 1.0 (/ reduce-pct 100.0))))(reduced-price 120 25) ;=> 90.0(reduced-price 120 50) ;=> 60.0
(defn reduced-price [price reduce-pct] (let [one-percent (/ price 100.0) discount (* one-percent reduce-pct)] (- price discount)))
(defn reduced-price [price reduce-pct] (* price (- 1.0 (/ reduce-pct 100.0))))(reduced-price 120 25) ;=> 90.0(reduced-price 120 50) ;=> 60.0
(defn reduced-price [price reduce-pct] (let [one-percent (/ price 100.0) discount (* one-percent reduce-pct)] (- price discount)))
(defn reduced-price [price reduce-pct] (let [one-percent (/ price 100.0) discount (* one-percent reduce-pct)] (- price discount)))
(defn major-sale [price] (reduced-price price 40))
(map major-sale [120.0 100.0 30.0]) ;=> (72.0 60.0 18.0)