From 3fbb3b5e188193dff8bc5a69c2317bb61de00a61 Mon Sep 17 00:00:00 2001 From: Alvaro Garcia Date: Wed, 21 Oct 2015 10:56:37 +0200 Subject: [PATCH 1/4] suggestion: convert to markdown As this is going to be executed in the REPL via emacs, prefer having a default "good-looking" format on the file --- src/intro-clojure/{basics.clj => basics.md} | 57 +++++++++++++++------ 1 file changed, 40 insertions(+), 17 deletions(-) rename src/intro-clojure/{basics.clj => basics.md} (88%) diff --git a/src/intro-clojure/basics.clj b/src/intro-clojure/basics.md similarity index 88% rename from src/intro-clojure/basics.clj rename to src/intro-clojure/basics.md index 55467c7..8d0bd81 100644 --- a/src/intro-clojure/basics.clj +++ b/src/intro-clojure/basics.md @@ -1,32 +1,49 @@ +# Basics + + +```lisp (ns intro-clojure.basics) +``` + +We'll attempt to learn a bit of clojure by write expressions and evaluate them on the REPL (read eval print loop) -(comment +## Expression/Forms can be send to REPL for evalutation - ;; We'll attempt to learn a bit of clojure by write - ;; expressions and evaluate them. - ;; REPL (read eval print loop) - ;; Expression/Forms can be send to REPL for evalutation +### Reader Forms (forms read by reader before eval) +#### Literals +##### strings - ;; Reader Forms (forms read by reader before eval) - ;; Literals - ;; strings +```lisp "hello world" - ;; characters +``` + +##### characters + +```lisp \h \newline - ;; numbers - ;; integers +``` +##### numbers + +```lisp + ;;integers 42 123412341234123412341234N ;; decimals 42.42 123412431234.12431243213M - ;; rational + ;; rationals 3/4 - ;; booleans +``` +##### booleans + +```lisp true false - ;; nothing, null +``` +##### nothing, null + +```lisp nil ;; keywords :keyword @@ -37,8 +54,11 @@ inc + hola +``` + +### Lists - ;; Lists +```lisp ;; "," are not used do separate elements -> white spaces instead. () '(1 2 3) @@ -55,8 +75,9 @@ ;; they can be nested [1 2 [3 4] {:one 1}] +``` - ;; Evaluation +### Evaluation ;; every form evaluates to itself except list. ;; list are evaluated as (operator operand1 operand2 ... operandn) ;; An operator can be a #{function special-form macro} @@ -64,6 +85,8 @@ ;; error not a operator -> quote to stop evaluation ;; ??? should we talk about quote (quote (1 2 3)) '(1 2 3) + +```lisp (1 2 3) (inc 1) @@ -205,4 +228,4 @@ ;; squares (take 10 (map #(* % %) (range))) - ) +``` From 1345846cc58473ffc6f184f1cc2e7baa79600355 Mon Sep 17 00:00:00 2001 From: Alvaro Garcia Date: Wed, 21 Oct 2015 10:59:33 +0200 Subject: [PATCH 2/4] markdown part 'evaluation' --- src/intro-clojure/basics.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/intro-clojure/basics.md b/src/intro-clojure/basics.md index 8d0bd81..2a3d9bd 100644 --- a/src/intro-clojure/basics.md +++ b/src/intro-clojure/basics.md @@ -78,13 +78,14 @@ We'll attempt to learn a bit of clojure by write expressions and evaluate them o ``` ### Evaluation - ;; every form evaluates to itself except list. - ;; list are evaluated as (operator operand1 operand2 ... operandn) - ;; An operator can be a #{function special-form macro} - ;; prefix notation - ;; error not a operator -> quote to stop evaluation - ;; ??? should we talk about quote (quote (1 2 3)) '(1 2 3) + * every form evaluates to itself except ``list``. + * list are evaluated as (operator operand1 operand2 ... operandn) + + An operator can be a ``#{function special-form macro}`` prefix notation + + error not a operator -> quote to stop evaluation + ??? should we talk about quote (quote (1 2 3)) '(1 2 3) ```lisp (1 2 3) From b415ac884bc8f967a8ad077be4eccd54a9b21ba4 Mon Sep 17 00:00:00 2001 From: Alvaro Garcia Date: Wed, 21 Oct 2015 11:00:51 +0200 Subject: [PATCH 3/4] remove all spaces in the big block --- src/intro-clojure/basics.md | 280 ++++++++++++++++++------------------ 1 file changed, 140 insertions(+), 140 deletions(-) diff --git a/src/intro-clojure/basics.md b/src/intro-clojure/basics.md index 2a3d9bd..d9780cc 100644 --- a/src/intro-clojure/basics.md +++ b/src/intro-clojure/basics.md @@ -88,145 +88,145 @@ We'll attempt to learn a bit of clojure by write expressions and evaluate them o ??? should we talk about quote (quote (1 2 3)) '(1 2 3) ```lisp - (1 2 3) - - (inc 1) - (+ 1 1) - (+ 1 2 3 4) - (str "hello" " " "world") - (class "hello") - (class 3/4) - - ;; nested function evaluate - (inc (inc 1)) - - ;; using symbols - ;; def is a special form - ;; more info (doc def) - (def sym1 "value1") - - ;; functions - ;; fn is a special form - ;; more info (doc fn) - (fn [x] (+ 1 x)) - ((fn [x] (+ 1 x)) 3) - - ;; name a function - ;; use a symbol to refer to function - (def my-inc (fn [x] (+ x 1))) - my-inc - (my-inc 1) - - ;; there is a macro to create a function and give it a name. - ;; (doc defn) - (defn my-inc2 [x] (+ x 1)) - (macroexpand '(defn my-inc2 [x] (+ x 1))) - - ;; let - ;; evaluate expressions with bind symbols - (let [x 1 - y 2] - (+ x y)) - - ;; example "scope" - (def x 40) - (let [x 1] (inc x)) - (inc x) - - ;; do - ;; do allow to group more than one expression - ;; used for side effects. - ;; error - ((+ 1 1) (+ 2 3)) - (do (+ 1 1) (+ 2 3)) - ;; side effects - (do (println "hello") - (println "bye")) - - - ;; flow control - ;; if special form - (if true "t" "f") - ;; nil is false - (if nil "t" "f") - ;; false and nil are false, anything else true - (if "everything else" "t" "f") - ;; even the empty collection (list, vector, map...) is true! - (if [] "t" "f") - - ;; other macros for flow control - ;; when - ;; cond - ;; etc - - ;; loops in clojure - ;; loop/recur Special forms - ;; tail recusive - (loop [x 10 fact 1] - (if (zero? x) fact - (recur (dec x) (* x fact)))) - - ;; fn - ;; functions - ;; fn is a forma especial. - ;; more info (doc fn) - (fn [x] (+ 1 x)) - ;; we can use the function we just defined - ((fn [x] (+ 1 x)) 3) - - ;; giving a name to a function - ;; we use a symbol to refer to it. - (def my-inc (fn [x] (+ x 1))) - my-inc - (my-inc 1) - - ;; There is a macro to create named functions - ;; (doc defn) - (defn my-inc2 [x] (+ x 1)) - (macroexpand '(defn my-inc2 [x] (+ x 1))) - - ;; once we name a function we could call it again. - (defn fact [x] - (if (zero? x) 1 - (* x (fact (dec x))))) - (fact 10) - ;; it's possible to name anomimous functions as well - ;; normally to call themselves - ((fn fact [x] - (if (zero? x) 1 - (* x (fact (dec x))))) 10) - - ;; we can call recur without a loop - ;; it will call the function we are in. - (defn fact[x acc] - (if (zero? x) acc - (recur (dec x) (* x acc)))) - (fact 10 1) - - ;; using named fn instead of loop recur - (defn fact [x] - ((fn fact[x acc] - (if (zero? x) acc - (recur (dec x) (* x acc)))) x 1)) - (fact 10) - - ;; high order functions - ;; functions that take other functions as arguments - - ;; fact - (defn fact [x] - (reduce * (range 1 x))) - (fact 10) - ;; curiosity why does it work for 0 - (fact 0) - (*) - (+) - - ;; count a's in a sentence - ;; ??? should #(%) be introduced? - (count (filter #(= \a %) "mama")) - - ;; squares - (take 10 (map #(* % %) (range))) +(1 2 3) + +(inc 1) +(+ 1 1) +(+ 1 2 3 4) +(str "hello" " " "world") +(class "hello") +(class 3/4) + +;; nested function evaluate +(inc (inc 1)) + +;; using symbols +;; def is a special form +;; more info (doc def) +(def sym1 "value1") + +;; functions +;; fn is a special form +;; more info (doc fn) +(fn [x] (+ 1 x)) +((fn [x] (+ 1 x)) 3) + +;; name a function +;; use a symbol to refer to function +(def my-inc (fn [x] (+ x 1))) +my-inc +(my-inc 1) + +;; there is a macro to create a function and give it a name. +;; (doc defn) +(defn my-inc2 [x] (+ x 1)) +(macroexpand '(defn my-inc2 [x] (+ x 1))) + +;; let +;; evaluate expressions with bind symbols +(let [x 1 + y 2] + (+ x y)) + +;; example "scope" +(def x 40) +(let [x 1] (inc x)) +(inc x) + +;; do +;; do allow to group more than one expression +;; used for side effects. +;; error +((+ 1 1) (+ 2 3)) +(do (+ 1 1) (+ 2 3)) +;; side effects +(do (println "hello") + (println "bye")) + + +;; flow control +;; if special form +(if true "t" "f") +;; nil is false +(if nil "t" "f") +;; false and nil are false, anything else true +(if "everything else" "t" "f") +;; even the empty collection (list, vector, map...) is true! +(if [] "t" "f") + +;; other macros for flow control +;; when +;; cond +;; etc + +;; loops in clojure +;; loop/recur Special forms +;; tail recusive +(loop [x 10 fact 1] + (if (zero? x) fact + (recur (dec x) (* x fact)))) + +;; fn +;; functions +;; fn is a forma especial. +;; more info (doc fn) +(fn [x] (+ 1 x)) +;; we can use the function we just defined +((fn [x] (+ 1 x)) 3) + +;; giving a name to a function +;; we use a symbol to refer to it. +(def my-inc (fn [x] (+ x 1))) +my-inc +(my-inc 1) + +;; There is a macro to create named functions +;; (doc defn) +(defn my-inc2 [x] (+ x 1)) +(macroexpand '(defn my-inc2 [x] (+ x 1))) + +;; once we name a function we could call it again. +(defn fact [x] + (if (zero? x) 1 + (* x (fact (dec x))))) +(fact 10) +;; it's possible to name anomimous functions as well +;; normally to call themselves +((fn fact [x] + (if (zero? x) 1 + (* x (fact (dec x))))) 10) + +;; we can call recur without a loop +;; it will call the function we are in. +(defn fact[x acc] + (if (zero? x) acc + (recur (dec x) (* x acc)))) +(fact 10 1) + +;; using named fn instead of loop recur +(defn fact [x] + ((fn fact[x acc] + (if (zero? x) acc + (recur (dec x) (* x acc)))) x 1)) +(fact 10) + +;; high order functions +;; functions that take other functions as arguments + +;; fact +(defn fact [x] + (reduce * (range 1 x))) +(fact 10) +;; curiosity why does it work for 0 +(fact 0) +(*) +(+) + +;; count a's in a sentence +;; ??? should #(%) be introduced? +(count (filter #(= \a %) "mama")) + +;; squares +(take 10 (map #(* % %) (range))) ``` From 1af400a4e99ee9bf6a10fb7c49f1f8900e8e87eb Mon Sep 17 00:00:00 2001 From: Alvaro Garcia Date: Wed, 21 Oct 2015 11:03:03 +0200 Subject: [PATCH 4/4] add idea on map syntax --- src/intro-clojure/basics.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/intro-clojure/basics.md b/src/intro-clojure/basics.md index d9780cc..4d29052 100644 --- a/src/intro-clojure/basics.md +++ b/src/intro-clojure/basics.md @@ -229,4 +229,19 @@ my-inc ;; squares (take 10 (map #(* % %) (range))) +;; explain map syntax + +;; remember +(= + (inc 1) + 2) + +;;simpler +(map inc [1 2 3]) + +;;more difficult +(take 10 (map inc (range))) + + + ```