Last active
          August 29, 2015 14:08 
        
      - 
      
 - 
        
Save bendisposto/0b705efefe4bc7683eaa to your computer and use it in GitHub Desktop.  
    vl_30_10_2014
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | ;; ---------- Homoiconicity | |
| (+ 1 2 3) | |
| '(+ 1 2 3) | |
| '(+ 1 x 3) | |
| (map type '(+ 1 x :foo)) | |
| (list + 1 2 3) | |
| (eval (list + 1 2 3)) | |
| ;; ---------- Definition | |
| pi | |
| (def pi 3) | |
| pi | |
| ;; ---------- Immutability | |
| (def v [1 2 3]) | |
| (concat v [4 5 6]) | |
| v | |
| ;; ---------- Funktionen | |
| ;; λx.x+3 | |
| (fn [x] (+ 3 x)) | |
| ;; --------- Definitionen cont'd | |
| (def π 3.141592653589793238M) | |
| (* 2 π) | |
| (def square (fn [x] (* x x))) | |
| (def sum-square (fn [x y] (+ (square x) (square y)))) | |
| ;; defn ist syntaktischer Zucker | |
| (defn sq2 [x] (* x x)) | |
| (sq2 15) | |
| (macroexpand-1 '(defn sq2 [x] (* x x))) | |
| ;; Funktionsauswertung | |
| ;; Mechanismus: beta reduction | |
| ;; Um eine Anwendung auszuwerten | |
| ;; 1) werte das erste Element aus um die Funktion zu erhalten | |
| ;; 2) werte die restlichen Elemente aus um die Argumente zu erhalten | |
| ;; 3) wende die Funktion auf die Argumente an | |
| ;; - kopiere den Funktionskörper substituiere dabei die formalen | |
| ;; parameter durch die Operanden | |
| ;; - werte den resultierenden neuen Körper aus | |
| ;; Beispiel Summe der Quadratzahlen | |
| (sum-square 3 4) | |
| (+ (square 3) (square 4)) | |
| (clojure.core/+ (square 3) (square 4)) | |
| (+ (square 3) (* 4 4)) | |
| (+ (square 3) 16) | |
| (+ (* 3 3) 16) | |
| (+ 9 16) | |
| 25 | |
| ;; ---------- Control structures | |
| (if true 1 2) | |
| (if false 1 2) | |
| (defn myif [b t e] (if b t e)) | |
| (myif false 1 2) | |
| (myif false (do (println :hello) :hello) | |
| (do (println :world) :world | |
| )) | |
| (if 1 2 3) | |
| (if [] 1 2) | |
| (if nil 1 2) | |
| (cond | |
| (= 1 2) 1 | |
| ( < 1 2) 2 | |
| :else 3) | |
| (cond | |
| (= 1 2) 1 | |
| ( > 1 2) 2 | |
| :otherwise 3) | |
| (macroexpand-1 '(if 1 2 3)) | |
| (macroexpand-1 '(cond (= 1 2) 2 (= 2 2) 4)) | |
| (macroexpand-1 '(if-not false 1 2)) | |
| ;; ---------- fac | |
| (defn ! [n] | |
| (if (= 1 n) | |
| n | |
| (* n (! (dec n))))) | |
| (! 10) | |
| (! 30) | |
| (defn ! [n] | |
| (if (= 1 n) | |
| n | |
| (*' n (! (dec n))))) | |
| (! 10000) | |
| (defn ! [n] (reduce *' (range 1 (inc n)))) | |
| (doc reduce) | |
| (time (let [x (! 10000)] :ok)) | |
| ;; fn als param | |
| (defn evaluate1 [f v] (f v)) | |
| (evaluate1 inc 12) | |
| ;; fn als wert | |
| (defn mk-adder [n] (fn [x] (+ x n))) | |
| (def foo (mk-adder 17)) | |
| (foo 4) | |
| ;; Listen | |
| (set! *print-length* 40) | |
| (range 10) | |
| (range 10 20) | |
| (range 1 100 10) | |
| ;; woah! unendliche Listen | |
| (range) | |
| ;; hof: map, filter, reduce | |
| (map inc (range 2 7)) | |
| (map + [1 2 3] [3 4 5] [2 3]) | |
| (map (fn [x] (* 2 x)) (range)) | |
| (filter (fn [x] (< 2 x 6)) [1 2 3 4 5 6]) | |
| (reduce * 1 (range 2 7)) | |
| (reduce * (range 2 7)) | |
| (reduce + 0 (range 2 7)) | |
| (reduce + (range 2 7)) | |
| (reduce conj [] [1 2 3]) | |
| ;; seq | |
| (first [1 2 3]) | |
| (first (list 1 2 3)) | |
| (first #{2 1 3}) | |
| (rand-nth (seq #{2 3 4 5 6})) | |
| (first {:b 2 :c 3 :a 1}) | |
| (first []) | |
| (first nil) | |
| (type (rest [1 2 3])) | |
| (type (rest (list 1 2 3))) | |
| (type (rest #{4 3 2 1})) | |
| (rest {:a 1 :b 2 :c 3}) | |
| (conj (rest {:a 2}) [:1 1]) | |
| (rest []) | |
| (rest nil) | |
| (cons 1 [2]) | |
| (cons 2 3) | |
| (conj [2 3] 1) | |
| (conj (list 2 3) 1) | |
| (defn mm [ f v] (into (empty v) (map f v))) | |
| (mm inc [1 2 3]) | |
| (mm inc '(1 2 3)) | |
| (mm inc #{1 2 4}) | |
| (def x (time (into [:a :b] (range 100000000)))) | |
| (type x) | |
| (time (last x)) | |
| (time (last x)) | |
| (def y (into '() x)) | |
| (count x) | |
| (def y (time (seq x))) | |
| (type y) | |
| (time (last y)) | |
| ;; maps | |
| (keys {:a 1 :b 2 "foo" 3 nil 4}) | |
| (assoc {:a 1} :b 2) | |
| (dissoc {:a 1} :a) | |
| (dissoc {:a 1} :b) | |
| {:a 1 :b 2 :c 3} | |
| (assoc {:a 1 :b 2} :c 3) | |
| (= {:a 1 :b 2 :c 3} (assoc {:a 1 :b 2} :c 3)) | |
| (get {:a 1, :b 2} :b) | |
| ({:a 1, :b 2} :b) | |
| (:b {:a 1 :b 2}) | |
| (:c {:c 1} "default") | |
| (:c {:a 123} "default") | |
| ;; for | |
| (for [user ["Itchy" "Scratchy"]] | |
| (str user " for president!")) | |
| (for [x [1 2 3 4] y [1 2 3 4]] [:tuple x y]) | |
| (for [x [1 2 3 4] y [1 2 3 4] :when (<= y x)] [:tuple x y]) | |
| ;; do | |
| (if true (do (println "true") 1) 2) | |
| (reverse [1 2 3]) | |
| (concat [1 2 3] [4 5 6]) | |
| (count [1 1 1 1]) | |
| (interleave [1 1 1 1] [2 2 2 2] [3 3 3 3]) | |
| (interleave [1 1 1 1] [2 2]) | |
| (interpose "1" [2 2 2 2 2 2]) | |
| (repeat 5 :a) | |
| (take 4 (range)) | |
| (take 5 (repeat "a")) | |
| (take 10 (repeat "a")) | |
| (drop 5 (range 14)) | |
| (take-while neg? (range -10 10000)) | |
| (drop-while neg? (range -10 10)) | |
| (last [1 2 3]) | |
| (butlast [1 2 3]) | |
| (first [1 2 3]) | |
| (second [1 2 3]) | |
| (nth [1 2 3] 2) | |
| (nth (range 1000) 33) | |
| (range 13) | |
| (partition 3 (range 13)) | |
| (partition 3 2 (range 13)) | |
| (partition 3 3 (repeat :a) (range 13)) | |
| (partition 3 3 [] (range 13)) | |
| (take 20 (cycle [1 2 3])) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment