Clojure 3 Recursion Higherorderfunctions 02 Nov20 Running Clojure


![Functions n The syntax to define a named function is: (defn function_name docstring? [arguments] Functions n The syntax to define a named function is: (defn function_name docstring? [arguments]](https://slidetodoc.com/presentation_image/40947b087447c668f495bd913305cb63/image-3.jpg)









![Anonymous functions n n n An anonymous function has the syntax: (fn [parameters] body) Anonymous functions n n n An anonymous function has the syntax: (fn [parameters] body)](https://slidetodoc.com/presentation_image/40947b087447c668f495bd913305cb63/image-13.jpg)


![reduce n n n user=> (defn my-reduce [f init lst] (cond (empty? lst) init reduce n n n user=> (defn my-reduce [f init lst] (cond (empty? lst) init](https://slidetodoc.com/presentation_image/40947b087447c668f495bd913305cb63/image-16.jpg)

![Closures I n n user=> (defn rangechecker [min max] (fn [num] (and (>= num Closures I n n user=> (defn rangechecker [min max] (fn [num] (and (>= num](https://slidetodoc.com/presentation_image/40947b087447c668f495bd913305cb63/image-18.jpg)
![Closures II n n user=> (defn adjust-range [min max] (fn [num] (cond (< num Closures II n n user=> (defn adjust-range [min max] (fn [num] (cond (< num](https://slidetodoc.com/presentation_image/40947b087447c668f495bd913305cb63/image-19.jpg)



- Slides: 22
Clojure 3 Recursion, Higher-order-functions 02 -Nov-20
Running Clojure n C: UsersDave>cd C: Programsclojure-1. 2. 0>java -cp clojure. jar clojure. main Clojure 1. 2. 0 user=> (load-file "E: /Programming/Clojure programs on E/test. clj") #'user/fruit user=> n n Although j. Edit has a clojure mode, I don’t like it n n n The response, #'user/fruit, is because “fruit” was the last thing defined on this file Syntax coloring is good, but indentation is strange (buggy? ) and agressive The lisp mode doesn’t syntax color as well, but indents properly Comments: n n 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
Functions n The syntax to define a named function is: (defn function_name docstring? [arguments] n n The value of the function is the value of the last expression evaluated The syntax of a function call is (function arguments) n n n expressions) 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 A Clojure function can be overloaded (have different bodies for different parameter lists) n Syntax: (defn function_name docstring? ([arguments] expressions). . . ([arguments] expressions) ) 3
Tail recursion n n A recursive function is one that calls itself A recursive function typically has three parts: 1. 2. 3. n One or more base cases that solve the simplest cases of the problem without recurring Recursive calls to solve subproblems that are simpler but of the same type as the given problem Code to augment solutions to subproblems into solutions of the given problem A function is tail recursive if the recursive call is the very last thing that the function does n n In other words, part 3 above is empty—no additional work need be done If the function has multiple exits, it is tail recursive only if every recursive call returns without doing additional work
Façades n A façade is a function that provides a different (usually better) interface to another function n n In Clojure it’s easy to define a function within another function n n Often this means supplying arguments that are only used for initialization This allows an “uglier” version of the function to be hidden Functions can also be overloaded 5
Tail recursion (Erlang) n n 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).
Tail recursion (Clojure) n n Non-tail recursive function to find the length of a list: (defn len-1 [lst] (if (empty? lst) 0 (inc (len-1 (rest lst))) ) ) Tail recursive function to find the length of a list: (defn len-2 ([lst] (len-2 0 lst)) ([n lst] (if (empty? lst) n (len-2 (inc n) (rest lst)) )
recur n n n 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-2 ([lst] (len-2 0 lst)) ([n lst] (if (empty? lst) n (recur (inc n) (rest lst))) ) )
Tail recursion (Erlang) n Non-tail recursive function to find the factorial: factorial(1) -> 1; factorial(N) -> N * factorial(N - 1). n Tail recursive function to find the factorial: factorial(N) -> factorial(1, N). factorial(Acc, 1) -> Acc; factorial(Acc, N) -> factorial(N * Acc, N - 1).
Tail recursion (Clojure) n Non-tail recursive function to find the factorial: n Tail recursive function to find the factorial: (defn factorial-1 [n] (if (= n 1) 1 (* n (factorial-1 (dec n))) ) (defn factorial-2 ([n] (factorial-2 1 n)) ([acc n] (if (= n 1) acc (recur (* n acc) (dec n)) )
Functions that take functions n n Functional programming languages allow you to use a function as a parameter to another function Almost all functional programming languages provide these three extremely useful functions: n n n map applies a function to every element of a sequence, giving a sequence of the results filter applies a predicate to every element of a sequence, giving a sequence of the results reduce (or fold) applies a binary operation “between” each pair of elements, giving a single (non-sequence) result n There are often different versions for left- and right-associative operations, and for providing or not providing an initial value 11
map n n (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)
Anonymous functions n n n An anonymous function has the syntax: (fn [parameters] body) Example: (fn [x] (* x x)) Anonymous functions are used when n n The function body is short and simple, and It is only needed in one place
filter n n (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))
Speaking of maps… n A map or hash is a sequence of key/value pairs, enclosed in braces, for example, {: ace 1, : deuce 2, "trey" 3} n n n A map is also a function: n n n Elements are separated by whitespace or commas It is helpful to use commas between key/value pairs user=> (def cards {: ace 1, : deuce 2, "trey" 3}) #'user/cards user=> (cards : deuce) 2 Keywords are also functions: n user=> (: deuce 2 cards)
reduce n n n user=> (defn my-reduce [f init lst] (cond (empty? lst) init : else (recur f (f init (first lst)) (rest lst)) ) ) #'user/my-reduce user=> (my-reduce + 0 '(11 22 33)) 66 user=> (my-reduce * 1 '(2 3 4)) 24
Functions that return functions n n n A closure is a function that contains and works with, not only its parameters, but values from the scope in which it was defined Currying creates a new function with fewer arguments than the original function Function composition combines two or more functions into a new function 17
Closures I n n user=> (defn rangechecker [min max] (fn [num] (and (>= num min) (<= num max))) ) #'user/rangechecker user=> (def in-range? (rangechecker 0 100)) #'user/in-range? user=> (in-range? 75) true user=> (in-range? 101) false
Closures II n n user=> (defn adjust-range [min max] (fn [num] (cond (< num min) min (> num max) max : else num )) ) #'user/adjust-range user=> (def adjust (adjust-range 0 100)) #'user/adjust user=> (adjust 75) 75 user=> (adjust 101) 100
Partial functions (currying) n n n (partial f arg 1 arg 2 arg 3 & more) Takes a function f and fewer than the normal arguments to f, and returns a fn that takes a variable number of additional args. When called, the returned function calls f with args + additional args. user=> (def hundred-times (partial * 100)) #'user/hundred-times user=> (hundred-times 5) 500
Function composition n n user=> (def negtimes (comp - *)) #'user/negtimes user=> (negtimes 5 3) -15 user=> (def third (comp first rest)) #'user/third user=> (third [11 22 33 44]) 33 user=> (third '(: a : b : c : d)) : c
The End