Clojure 2 22-Apr-19.

Slides:



Advertisements
Similar presentations
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Advertisements

Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
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.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
Clojure Template Tail Recursion. Hello, Factorial! The factorial function is everybody’s introduction to recursion (defn factorial-1 [n] (if (zero? n)
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
Unit 2: Java Introduction to Programming 2.1 Initial Example.
1 (Functional (Programming (in (Scheme)))) Jianguo Lu.
Clojure 3 Recursion, Higher-order-functions 27-Aug-15.
17-Sep-15 Erlang. Running Erlang Double-click on the Erlang icon, or type erl into a terminal (cmd) window You can try short pieces of code from here,
Clojure “Lisp on the JVM”. 2 Versions of LISP Lisp is an old language with many variants LISP is an acronym for List Processing language Invented by John.
Comments are for people Header comments supply basic information about the artifact.
Λ => Scheme for Rubyists. Scheme History Authors: Guy Steele and Gerald Sussman Structure and Interpretation of Computer Programs (SICP) by Abelson &
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Scheme & Functional Programming. ( ) >> 64 ( ) >> 666 (* ) >> 1200 (+ (* 3 5) (- 10 6)) >> 19.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
Computer Science 101 Introduction to Programming.
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.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
Clojure 2 Feb 7,
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
Controlling Execution Dong Shao, Nanjing Unviersity.
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.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
Compiling Functional Programs Mooly Sagiv Chapter 7
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler.
Clojure “Lisp Reloaded”. 2 Versions of LISP Lisp is an old language with many variants LISP is an acronym for List Processing language Lisp is alive and.
CMSC 330: Organization of Programming Languages
Basic Introduction to Lisp
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Fall 2016 Images from Sebesta: Copyright © 2012 Addison-Wesley. All rights reserved. The mind is everything. What you think you become. Buddha.
Fall 2016 Images from Sebesta: Copyright © 2012 Addison-Wesley. All rights reserved.
Lets and Loops Tail Recursion.
Clojure “Lisp Reloaded”.
Section 15.4, 15.6 plus other materials
Introduction to Python
CS 326 Programming Languages, Concepts and Implementation
Halting Functions for Tree-Like Structures
Modern JavaScript Develop And Design
Important Concepts from Clojure
Important Concepts from Clojure
Clojure 6 Concurrency 20-Nov-18.
Clojure “Lisp Reloaded”.
Thinking in Clojure 23-Nov-18.
Clojure to Haskell (It’s mostly syntax).
Clojure 4 Sequences 27-Nov-18.
Functions and Macros.
Clojure 4 Sequences 5-Dec-18.
Thinking in Clojure 1-Jan-19.
Thinking in Clojure 1-Jan-19.
Scheme: Basic Functionality
Functional interface.
Clojure “Lisp Reloaded”.
Lisp, Then and Now.
Clojure “Lisp Reloaded”.
(more) Python.
Recursion, Higher-order-functions
Important Concepts from Clojure
CSC 143 Java Searching.
Clojure 3 1-Jun-19.
Thinking in Clojure 8-Jun-19.
Lisp.
Presentation transcript:

Clojure 2 22-Apr-19

Running Clojure Although jEdit has a clojure mode, I don’t like it C:\Users\Dave>cd C:\Programs\clojure-1.2.0 C:\Programs\clojure-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=> 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

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

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).

Tail recursion (Clojure) 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 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) 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).

Tail recursion (Clojure) 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)) ) ) )

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)

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

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))

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

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"