Clojure to Haskell Its mostly syntax Running Haskell

  • Slides: 14
Download presentation
Clojure to Haskell (It’s mostly syntax)

Clojure to Haskell (It’s mostly syntax)

Running Haskell n Download and install the Haskell Platform n Run Haskell interactively by

Running Haskell n Download and install the Haskell Platform n Run Haskell interactively by typing ghci at the command line prompt n n 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 n n Names of lists should end in ss Indentation is relevant : load name, : edit, : ? , : quit 2

Clojure and Haskell functions n n n (+ 2 (* 3 4)) (f a

Clojure and Haskell functions n n n (+ 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]) n n n 2 + 3 * 4 f a b head [1 2 3 4 5] tail [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] 3

Defining functions n n n n user=> (defn triple [x] (* 3 x)) #'user/triple

Defining functions n n n n 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> 30 let triple x = 3 * x triple 10 n Haskell requires the let only in GHCi; don’t put it in your program on a file let triple = x -> 3 * x triple 10 4

map n (map f coll & colls) n n n user=> (map inc [1

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

filter n (filter pred coll) n n user=> (filter even? (range 10)) (0 2

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

fold/reduce n (reduce f val coll) n n n user=> (reduce + [1 2

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

List comprehensions n n user=> (for [x [0 1 2 3 4 5] :

List comprehensions n n 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] 8

Closures n n n n user=> (defn rangechecker [min max] (fn [num] (and (>=

Closures n n 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? 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 9

Currying n Currying is absorbing a parameter into a function to make a new

Currying n Currying is absorbing a parameter into a function to make a new function n n user=> (def hundred-times (partial * 100)) #'user/hundred-times user=> (hundred-times 3) 300 GHCi> let hundred_times = (*) 100 GHCi> hundred_times 3 300 10

Function composition n Function composition is combining two or more functions into a new

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

Laziness n A lazy data structure is one where parts of it do not

Laziness n A lazy data structure is one where parts of it do not exist until they are accessed n n n 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] 12

I’m thinking of something. A. B. C. Animal Vegetable Mineral 13

I’m thinking of something. A. B. C. Animal Vegetable Mineral 13

The End 14

The End 14