Presentation is loading. Please wait.

Presentation is loading. Please wait.

Clojure 2 22-Apr-19.

Similar presentations


Presentation on theme: "Clojure 2 22-Apr-19."— Presentation transcript:

1 Clojure 2 22-Apr-19

2

3 Running Clojure Although jEdit has a clojure mode, I don’t like it
C:\Users\Dave>cd C:\Programs\clojure C:\Programs\clojure-1.2.0>java -cp clojure.jar clojure.main Clojure user=> (load-file "E:/Programming/Clojure programs on E/test.clj") #'user/fruit user=> The response, #'user/fruit, is because “fruit” was the last thing defined on this file Although jEdit has a clojure mode, I don’t like it Syntax coloring is good, but indentation is strange (buggy?) and agressive The lisp mode doesn’t syntax color as well, but indents properly Comments: A “doc” string is a string that goes just before the parameter list Other comments start with a semicolon (;) and extend to the end of the line

4 Functions The syntax to define a named function is: (defn function_name [arguments] expressions) The value of the function is the value of the last expression evaluated The syntax of a function call is (function arguments) Notice that the function being called is the first thing inside the parentheses This need not be the name of a function; it can be any expression that results in a function

5 Tail recursion (Erlang)
Non-tail recursive function to find the length of a list: len([]) -> 0; len([_ | T]) -> 1 + len(T). Tail recursive function to find the length of a list: len(L) -> len(0, L). len(N, []) -> N; len(N, [_ | T]) -> len(N + 1, T).

6 Tail recursion (Clojure)
Non-tail recursive function to find the length of a list: (defn len-1 [lst] (if (empty? lst) (inc (len-1 (rest lst))) ) ) Tail recursive function to find the length of a list: (defn len ([lst] (len-2 0 lst)) ([n lst] (if (empty? lst) n (len-2 (inc n) (rest lst))) ) )

7 recur The previous function, len-2, is tail-recursive, but the compiler doesn’t optimize it into a loop Clojure runs on the JVM, which doesn’t optimize tail recursion Workaround: (defn len ([lst] (len-2 0 lst)) ([n lst] (if (empty? lst) n (recur (inc n) (rest lst))) ) )

8 Tail recursion (Erlang)
Non-tail recursive function to find the factorial: factorial(1) -> 1; factorial(N) -> N * factorial(N - 1). Tail recursive function to find the factorial: factorial(N) -> factorial(1, N). factorial(Acc, 1) -> Acc; factorial(Acc, N) -> factorial(N * Acc, N - 1).

9 Tail recursion (Clojure)
Non-tail recursive function to find the factorial: (defn factorial-1 [n] (if (= n 1) (* n (factorial-1 (dec n))) ) Tail recursive function to find the factorial: (defn factorial ([n] (factorial-2 1 n)) ([acc n] (if (= n 1) acc (recur (* n acc) (dec n)) ) ) )

10 map (def fruit '((apple red) (banana yellow) (cherry red)))
user=> (map first fruit) (apple banana cherry) (defn my-map [f lst] (if (empty? lst) () (cons (f (first lst)) (my-map f (rest lst))) ) ) user=> (map my-first fruit) (apple banana cherry)

11 Anonymous functions An anonymous function has the syntax: (fn [parameters] body) Example: (fn [x] (* x x))

12 filter (def fruit '((apple red) (banana yellow) (cherry red)))
user=> (filter (fn [x] (= (second x) 'red)) fruit) ((apple red) (cherry red)) (defn my-filter [p lst] (cond (empty? lst) () (p (first lst)) (cons (first lst) (my-filter p (rest lst))) :else (my-filter p (rest lst)) ) ) user=> (my-filter (fn [x] (= (second x) 'red)) fruit) ((apple red) (cherry red))

13 Speaking of maps… A map or hash is a sequence of key/value pairs, enclosed in braces, for example, {:ace 1, :deuce 2, "trey" 3} Elements are separated by whitespace or commas It is helpful to use commas between key/value pairs A map is also a function: user=> (def cards {:ace 1, :deuce 2, "trey" 3}) #'user/cards user=> (cards :deuce) 2 Keywords are also functions: user=> (:deuce cards) 2

14 structs A struct is something like a Java class
(defstruct book :title :author) (def b (struct book "Small Gods" "Terry Pratchett")) A struct is something like a map (:author b) returns "Terry Pratchett"


Download ppt "Clojure 2 22-Apr-19."

Similar presentations


Ads by Google