Download presentation
Presentation is loading. Please wait.
1
Clojure to Haskell (It’s mostly syntax)
2
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
3
Clojure and Haskell functions
(+ 2 (* 3 4)) (f a b) (first[ ]) (rest [ ]) (nth [ ] 2) (concat [1 2 3] [4 5]) (take 3 [ ]) (drop 3 [ ]) (count [ ]) (apply + [ ]) (apply * [ ]) (reverse [ ]) 2 + 3 * 4 f a b head [ ] tail [ ] [ ] !! 2 [1 2 3] ++ [4 5] take 3 [ ] drop 3 [ ] length [ ] sum [ ] product [ ] reverse [ ]
4
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
5
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]
6
filter (filter pred coll) filter :: (a -> Bool) -> [a] -> [a]
user=> (filter even? (range 10)) ( ) filter :: (a -> Bool) -> [a] -> [a] GHCi> filter even [1..10] [2,4,6,8,10]
7
fold/reduce (reduce f val coll)
user=> (reduce + [ ]) 10 user=> (reduce [ ]) 1010 filter :: (a -> Bool) -> [a] -> [a] GHCi> filter even [1..10] [2,4,6,8,10]
8
List comprehensions user=> (for [x [ ] :let [y (* x 3)] :when (even? y)] y) (0 6 12) GHCi> [x * 3 | x <- [1..5], even x] [0,6,12]
9
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
10
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
11
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
12
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)) ( ) GHCi> take 4 [1..] [1,2,3,4]
13
I’m thinking of something.
Animal Vegetable Mineral 16
14
The End
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.