Clojure to Haskell (It’s mostly syntax).

Slides:



Advertisements
Similar presentations
Getting Started with Haskell Tim Sheard. Learning a new language Goals – Learn how to design and use data structures – Learn to write programs using the.
Advertisements

Java Programming Working with TextPad. Using TextPad to Work with Java This text editor is designed for working with Java You can download a trial version.
0 PROGRAMMING IN HASKELL Chapter 2 - First Steps.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
PYTHON: LESSON 1 Catherine and Annie. WHAT IS PYTHON ANYWAY?  Python is a programming language.  But what’s a programming language?  It’s a language.
Clojure 3 Recursion, Higher-order-functions 27-Aug-15.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
0 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Definitions. name :: Type answer :: Int name = expression answer = Definitions associate a name with a value of a certain type is of type greater.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
Haskell Starting Out Piotr Poniatowski Łukasz Reszczyński Maciej Woźniczka.
0 PROGRAMMING IN HASKELL Some first steps Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
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,
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
Overview of the Haskell 98 Programming Language
0 Functors in Haskell Adapted from material by Miran Lipovaca.
What is a Type? A type is a name for a collection of related values. For example, in Haskell the basic type Bool contains the two logical values: True.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
Introduction to Objective Caml. General comments ML is a purely functional language--there are (almost) no side effects There are two basic dialects of.
Haskell Basics CSCE 314 Spring CSCE 314 – Programming Studio Using GHC and GHCi Log in to unix.cse.tamu.edu (or some other server) From a shell.
0 PROGRAMMING IN HASKELL Chapter 2 - First Steps.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
Haskell. GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
1 PROGRAMMING IN HASKELL Lecture 2 Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
Polymorphic Functions
Midterm recap Total was 80 points Distribution range
Programming Languages
dr Robert Kowalczyk WMiI UŁ
Types CSCE 314 Spring 2016.
Introduction to Python
PROGRAMMING IN HASKELL
Topics Introduction to Repetition Structures
PROGRAMMING IN HASKELL
Functional Programming
Haskell.
Basic operations in Matlab
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
Installation and exercises
Logical Operators and While Loops
Important Concepts from Clojure
Important Concepts from Clojure
Clojure 4 Sequences 27-Nov-18.
Programming Languages
Higher Order Functions
Clojure 4 Sequences 5-Dec-18.
Python programming exercise
Haskell Types, Classes, and Functions, Currying, and Polymorphism
Fundamentals of Functional Programming
PROGRAMMING IN HASKELL
Recursion, Higher-order-functions
Logical Operators and While Loops
Important Concepts from Clojure
Clojure 2 22-Apr-19.
Functional Programming
Functions and patterns
PROGRAMMING IN HASKELL
Functional Programming and Haskell
PROGRAMMING IN HASKELL
Clojure 3 1-Jun-19.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Functional Programming and Haskell
Presentation transcript:

Clojure to Haskell (It’s mostly syntax)

Running Haskell Download and install the Haskell Platform Run Haskell interactively by typing ghci at the command line prompt Use your favorite text editor to write some Haskell code, and save it in a file with the .hs extension Run Haskell with ghci filename When you make a change in the editor, save it and enter :reload into ghci Variables and function names must begin with a lowercase letter Names of lists should end in s Names of lists of lists should end in ss Indentation is relevant :load name, :edit name, :edit, :?, :quit

Clojure and Haskell functions (+ 2 (* 3 4)) (f a b) (first[1 2 3 4 5]) (rest [1 2 3 4 5]) (nth [1 2 3 4 5] 2) (concat [1 2 3] [4 5]) (take 3 [1 2 3 4 5]) (drop 3 [1 2 3 4 5]) (count [1 2 3 4 5]) (apply + [1 2 3 4 5]) (apply * [1 2 3 4 5]) (reverse [1 2 3 4 5]) 2 + 3 * 4 f a b head [1 2 3 4 5] tail [1 2 3 4 5] [1 2 3 4 5] !! 2 [1 2 3] ++ [4 5] take 3 [1 2 3 4 5] drop 3 [1 2 3 4 5] length [1 2 3 4 5] sum [1 2 3 4 5] product [1 2 3 4 5] reverse [1 2 3 4 5]

Defining functions user=> (defn triple [x] (* 3 x)) #'user/triple user=> (def triple (fn [x] (* 3 x))) #'user/triple user=> (def triple #(* 3 %)) #'user/triple user=> (triple 10) 30 GHCi> let triple x = 3 * x GHCi> triple 10 30 GHCi> let triple = \x -> 3 * x Haskell requires the let only in GHCi; don’t put it in your program on a file

map (map f coll & colls) map :: (a -> b) -> [a] -> [b] user=> (map inc [1 2 3]) (2 3 4) user=> (map + [1 2] [3 4]) (4 6) map :: (a -> b) -> [a] -> [b] GHCi> map succ [1, 2, 3] [2,3,4]

filter (filter pred coll) filter :: (a -> Bool) -> [a] -> [a] user=> (filter even? (range 10)) (0 2 4 6 8) filter :: (a -> Bool) -> [a] -> [a] GHCi> filter even [1..10] [2,4,6,8,10]

fold/reduce (reduce f val coll) user=> (reduce + [1 2 3 4]) 10 user=> (reduce + 1000 [1 2 3 4]) 1010 filter :: (a -> Bool) -> [a] -> [a] GHCi> filter even [1..10] [2,4,6,8,10]

List comprehensions user=> (for [x [0 1 2 3 4 5] :let [y (* x 3)] :when (even? y)] y) (0 6 12) GHCi> [x * 3 | x <- [1..5], even x] [0,6,12]

Closures 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? 44) true user=> (in-range? 101) false GHCi> let rangechecker min max = (\num -> (min <= num) && (num <= max)) GHCi> let in_range = rangechecker 0 100 GHCi> in_range 44 True GHCi> in_range 101 False

Currying 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 GHCi> let hundred_times = (*) 100 GHCi> hundred_times 3 300

Function composition Function composition is combining two or more functions into a new function user=> (def third (comp first rest rest)) #'user/third user=> (third [2, 4, 6, 8]) 6 GHCi> let third = head . tail . tail GHCi> third [2, 4, 6, 8] 6

Laziness A lazy data structure is one where parts of it do not exist until they are accessed Do not try to access the entire thing, or the last thing (because there is no last thing) user=> (take 4 (iterate inc 1)) (1 2 3 4) GHCi> take 4 [1..] [1,2,3,4]

I’m thinking of something. Animal Vegetable Mineral 16

The End