Important Concepts from Clojure

Slides:



Advertisements
Similar presentations
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Advertisements

Programming Languages and Paradigms
Types of Recursive Methods
More about functions Plus a few random things. 2 Tail recursion A function is said to be tail recursive if the recursive call is the very last thing it.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages It’s elegant, minimal, can be.
Clojure Template Tail Recursion. Hello, Factorial! The factorial function is everybody’s introduction to recursion (defn factorial-1 [n] (if (zero? n)
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Functional Programming
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
Clojure 3 Recursion, Higher-order-functions 27-Aug-15.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Structure and Interpretation of Computer Programs Presented by Yan Yan CSE 294, UCSD, April 13, Chapter Harold Abelson, Gerald and Julie.
CMSC 2021 Recursion Recursive Definition – one that defines something in terms of itself Recursion – A technique that allows us to break down a problem.
ISBN Chapter 15 Functional Programming Languages.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
Solving N-Queens in Clojure
Clojure 4 Sequences 20-Oct-15. Clojure errors (NO_SOURCE_FILE:12) Useless--just means you’re running from the REPL shell java.lang.Exception: EOF while.
Clojure 2 Feb 7,
Feb 7, 2015 Thinking in Clojure. Jumping in We’ll quickly go through Clojure’s data types, some basic functions, and basic syntax Then we’ll get to the.
PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler.
Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Interpreters and Higher-Order Functions CSE 413 Autumn 2008 Credit: CSE341 notes by Dan Grossman.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
Introduction to Functional Programming Part 1 – The Basic Concepts Winter Young
Clojure Macros. Homoiconicity All versions of Lisp, including Clojure, are homoiconic This means that there is no difference between the form of the data.
Functional Programming
Lets and Loops Tail Recursion.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2017.
Functional Programming Languages
Tail Recursion.
CS 3304 Comparative Languages
CSE 341 Lecture 5 efficiency issues; tail recursion; print
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Closures and Streams cs784(Prasad) L11Clos
Introduction to Functional Programming in Racket
Important Concepts from Clojure
Thinking in Clojure 23-Nov-18.
Clojure to Haskell (It’s mostly syntax).
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2018.
Clojure 4 Sequences 27-Nov-18.
FP Foundations, Scheme In Text: Chapter 14.
Clojure 4 Sequences 5-Dec-18.
Procedural vs Functional Style
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Zach Tatlock Winter 2018.
Thinking in Clojure 1-Jan-19.
Thinking in Clojure 1-Jan-19.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2016.
Introduction to Functional Programming in Racket
Clojure Macros.
Recursion, Higher-order-functions
Important Concepts from Clojure
The structure of programming
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2017.
Abstraction and Repetition
Clojure 2 22-Apr-19.
Closures and Streams cs7100(Prasad) L11Clos
The structure of programming
Thinking procedurally
Clojure 3 1-Jun-19.
Chapter 15 Functional Programming 6/1/2019.
Thinking in Clojure 8-Jun-19.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2019.
Presentation transcript:

Important Concepts from Clojure 14-Nov-18

Abstract syntax trees Compilers turn source code into ASTs (Abstract Syntax Trees) This is a major first step in virtually all compilers and interpreters Lisp/Clojure syntax is an AST

Basic functional programming Functions are values (“first-class objects”) May be passed as parameters to other functions May be returned as the result of a function call May be stored in variables or data structures Functions have no side effects Given the same parameters, a function returns the same result Referential transparency: The result of a function call may be substituted for the function call Data is immutable and persistent Immutable data makes concurrency much easier Persistent means that structure is shared

Recursion Loops and recursion are equivalent in power Anything that can be done with a loop can be done recursively, and vice versa If a function is tail-recursive, it can be automatically turned into a loop (thus saving stack frames) Tail recursion is when the recursion is the last thing done in every branch of the function Loops are less useful when data is immutable Loops are used primarily to modify data When data is immutable, the substitution rule applies: Any variable or function call may be replaced by its value The substitution rule simplifies reasoning about programs

Tail call elimination Recursive functions can often be rewritten to be tail recursive This is done by creating a helper function that takes an additional “accumulator” parameter Non-tail recursive function to find the factorial: (defn factorial-1 [n] (if (= n 1) 1 (* n (factorial-1 (dec n))) ) Tail recursive function to find the factorial: (defn factorial-2 ([n] (factorial-2 1 n)) ([acc n] (if (= n 1) acc (recur (* n acc) (dec n)) ) ) )

Higher-order functions add expressiveness A higher-order function is a function that takes a function as an argument, returns a function as its vallue, or both Of course, higher-order functions do not increase the number of things that can be computed Any Turing complete language can compute anything that can be computed It doesn’t take much for a language to be Turing complete Higher-order functions can take the place of explicit recursions (or explicit loops) Using higher-order functions typically makes code shorter and clearer

Common higher-order functions Almost every functional languages has these features and functions: Anonymous (literal) functions, so a function doesn’t have to be defined independently, but can be placed directly as an argument to a higher-order function (map function sequence) applies the function to each element of the sequence, yielding a sequence of the results (filter predicate sequence) applies the predicate to each element of the sequence, yielding a sequence of the elements that satisfy the predicate (reduce initial-value binary-function sequence) applies the binary-function to each pair of elements of the sequence, starting with aninitial-value , and yielding a single value List comprehensions combine and may simplify the above user=> (for [x (take 10 (iterate inc 1))] (* x x)) (1 4 9 16 25 36 49 64 81 100)

Closures When a function definition uses variables that are not parameters or local variables, those variables are “closed over” and retained by the function The function uses those variables, not the value the variables had when the function was created user=> (defn rangechecker [min max] (fn [num] (and (>= num min) (<= num max))) ) #'user/rangechecker Notice that the function being returned gets the values of min and max from the environment, not as parameters user=> (def in-range? (rangechecker 0 100)) #'user/in-range? user=> (in-range? 101) false

Currying and function composition Currying is absorbing a parameter into a function to make a new function user=> (def hundred-times (partial * 100)) #'user/hundred-times user=> (hundred-times 3) 300 Function composition is combining two or more functions into a new function user=> (def third (comp first rest rest)) #'user/third user=> (third [1 2 3 4 5]) 3

Persistence and laziness A persistent data structure is one that is itself immutable, but can be modified to create a “new” data structure The original and the new data structure share structure to minimize copying time and wasted storage A lazy data structure is one where parts of it do not exist until they are accessed They are implemented (by Clojure) as functions that return elements of a sequence only when requested This allows you to have “infinite” data structures

Macros, metaprogramming, homoiconicity A macro is like a function whose result is code Arguments to a macro are not evaluated Macro calls are evaluated at compile time The return value of a macro should be executable code Metaprogramming is using programs to write programs This is the primary use of macros in Clojure Macros are used to implement DSLs (Domain Specific Languages) Homoiconicity is when there is no distinction between code and data Homoiconicity greatly simplifies metaprogramming

The End