Beyond functional programming in Haskell: an introduction to OCaml

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Favorite

    Beyond functional programming in Haskell: an introduction to OCaml - Presentation Transcript

    1. Intro to OCaml Beyond functional programming in Haskell: an introduction to OCaml Michiel Overeem Center for Software Technology, Universiteit Utrecht December 21, 2006 Center for Software Technology Michiel Overeem
    2. Intro to OCaml Functional programming beyond Haskell We have all learned functional programming in Haskell, but there are more functional languages like Lisp, Scheme, ML, and Clean. Why should we even bother to look further than Haskell? Center for Software Technology Michiel Overeem
    3. Intro to OCaml Functional programming beyond Haskell We have all learned functional programming in Haskell, but there are more functional languages like Lisp, Scheme, ML, and Clean. Why should we even bother to look further than Haskell? You want your programs to run faster. Center for Software Technology Michiel Overeem
    4. Intro to OCaml Functional programming beyond Haskell We have all learned functional programming in Haskell, but there are more functional languages like Lisp, Scheme, ML, and Clean. Why should we even bother to look further than Haskell? You want your programs to run faster. Monads drive you mad (what are they anyway? warm fuzzy things?). Center for Software Technology Michiel Overeem
    5. Intro to OCaml Functional programming beyond Haskell We have all learned functional programming in Haskell, but there are more functional languages like Lisp, Scheme, ML, and Clean. Why should we even bother to look further than Haskell? You want your programs to run faster. Monads drive you mad (what are they anyway? warm fuzzy things?). You need objects. Center for Software Technology Michiel Overeem
    6. Intro to OCaml Functional programming beyond Haskell We have all learned functional programming in Haskell, but there are more functional languages like Lisp, Scheme, ML, and Clean. Why should we even bother to look further than Haskell? You want your programs to run faster. Monads drive you mad (what are they anyway? warm fuzzy things?). You need objects. You sometimes need a more powerful module system. Center for Software Technology Michiel Overeem
    7. Intro to OCaml OCaml vs Haskell in the ICFP OCaml performed very well in the previous ICFP contests: Ocaml Haskell First place: 2002, 2000, 1999 2006, 2005, 2004, 2001 Second place: 2000, 1998 2004 Third place: 2001 2005, 2000 Lightning division prize: 2003 Judge’s prize: 2004 (Source: caml.inria.fr and ICFP websites) It look likes OCaml’s days are over... Center for Software Technology Michiel Overeem
    8. Intro to OCaml Overview OCaml facts 1 First steps in OCaml 2 Side-effects 3 The O in OCaml 4 Large applications 5 Some last remarks 6 Conclusion 7 Center for Software Technology Michiel Overeem
    9. Intro to OCaml > OCaml facts Quick fact sheet Some facts about Caml (Categorical Abstract Machine Language) Created in 1987 by INRIA - France’s national research institute for computer science (Haskell 1.0 is from 1990) Originated from ML but was intended for in house projects of INRIA Short timeline: Caml (1987) → Caml light (1990) → OCaml (1995) Currently at version 3.09.3 (3.09 was released in october 2005) Center for Software Technology Michiel Overeem
    10. Intro to OCaml > First steps in OCaml Let’s start our OCaml tour We will start with writing an OCaml version of wc (word count). This will show us how to work with input and output, and how to structure our programs in OCaml. Let’s begin! Center for Software Technology Michiel Overeem
    11. Intro to OCaml > First steps in OCaml Counting words We want to count chars, words, and lines. How would we count words in Haskell? words in Haskell - from the Prelude words :: String → [String] words s = case dropWhile Char.isSpace s of ”” → [] s’ → w : words s” where (w, s”) = break Char.isSpace s’ Center for Software Technology Michiel Overeem
    12. Intro to OCaml > First steps in OCaml Counting words (2) How does this translate to OCaml? words in OCaml let rec words : string → string list = fun s → match dropWhile isSpace s with ”” → [] | s’ → let (w, s”) = break isSpace s’ in w :: words s”;; Center for Software Technology Michiel Overeem
    13. Intro to OCaml > First steps in OCaml Counting words (2) How does this translate to OCaml? words in OCaml let rec words : string → string list = fun s → match dropWhile isSpace s with ”” → [] | s’ → let (w, s”) = break isSpace s’ in w :: words s”;; Center for Software Technology Michiel Overeem
    14. Intro to OCaml > First steps in OCaml Counting words (2) How does this translate to OCaml? words in OCaml let rec words : string → string list = fun s → match dropWhile isSpace s with ”” → [] | s’ → let (w, s”) = break isSpace s’ in w :: words s”;; Center for Software Technology Michiel Overeem
    15. Intro to OCaml > First steps in OCaml Counting words (2) How does this translate to OCaml? words in OCaml let rec words : string → string list = fun s → match dropWhile isSpace s with ”” → [] | s’ → let (w, s”) = break isSpace s’ in w :: words s”;; Center for Software Technology Michiel Overeem
    16. Intro to OCaml > First steps in OCaml Counting words (2) How does this translate to OCaml? words in OCaml let rec words : string → string list = fun s → match dropWhile isSpace s with ”” → [] | s’ → let (w, s”) = break isSpace s’ in w :: words s”;; Center for Software Technology Michiel Overeem
    17. Intro to OCaml > First steps in OCaml Counting words (3) We could count the words in OCaml in a different way: words in OCaml - imperative let words’ : string → string list = fun input → let s = ref input in let words = ref [] in s := dropWhile isSpace !s; while String.length !s != 0 do let (w, s’) = break isSpace !s in words := !words @ [w]; s := s’ done ; !words;; We can program with loops and references in OCaml, but is it elegant? Center for Software Technology Michiel Overeem
    18. Intro to OCaml > First steps in OCaml Counting words (3) We could count the words in OCaml in a different way: words in OCaml - imperative let words’ : string → string list = fun input → let s = ref input in let words = ref [] in s := dropWhile isSpace !s; while String.length !s != 0 do let (w, s’) = break isSpace !s in words := !words @ [w]; s := s’ done ; !words;; We can program with loops and references in OCaml, but is it elegant? Center for Software Technology Michiel Overeem
    19. Intro to OCaml > First steps in OCaml Counting words (3) We could count the words in OCaml in a different way: words in OCaml - imperative let words’ : string → string list = fun input → let s = ref input in let words = ref [] in s := dropWhile isSpace !s; while String.length !s != 0 do let (w, s’) = break isSpace !s in words := !words @ [w]; s := s’ done ; !words;; We can program with loops and references in OCaml, but is it elegant? Center for Software Technology Michiel Overeem
    20. Intro to OCaml > First steps in OCaml Counting words (3) We could count the words in OCaml in a different way: words in OCaml - imperative let words’ : string → string list = fun input → let s = ref input in s : string ref let words = ref [] in s := dropWhile isSpace !s; while String.length !s != 0 do let (w, s’) = break isSpace !s in words := !words @ [w]; s := s’ done ; !words;; We can program with loops and references in OCaml, but is it elegant? Center for Software Technology Michiel Overeem
    21. Intro to OCaml > First steps in OCaml Counting words (3) We could count the words in OCaml in a different way: words in OCaml - imperative let words’ : string → string list = fun input → let s = ref input in let words = ref [] in s := dropWhile isSpace !s; while String.length !s != 0 do let (w, s’) = break isSpace !s in words := !words @ [w]; s := s’ done ; !words;; We can program with loops and references in OCaml, but is it elegant? Center for Software Technology Michiel Overeem
    22. Intro to OCaml > First steps in OCaml Counting words (3) We could count the words in OCaml in a different way: words in OCaml - imperative let words’ : string → string list = fun input → let s = ref input in let words = ref [] in s := dropWhile isSpace !s; while String.length !s != 0 do let (w, s’) = break isSpace !s in words := !words @ [w]; s := s’ done ; !words;; We can program with loops and references in OCaml, but is it elegant? Center for Software Technology Michiel Overeem
    23. Intro to OCaml > First steps in OCaml Counting words (3) We could count the words in OCaml in a different way: words in OCaml - imperative : string → are only = let words’Assignments string listallowed on references: = → ’a → unit fun input → let s : ’a ref ref input in ( := ) let words = ref [] in s := dropWhile isSpace !s; while String.length !s != 0 do let (w, s’) = break isSpace !s in words := !words @ [w]; s := s’ done ; !words;; We can program with loops and references in OCaml, but is it elegant? Center for Software Technology Michiel Overeem
    24. Intro to OCaml > First steps in OCaml Counting words (3) We could count the words in OCaml in a different way: words in OCaml - imperative let words’ : string → string list = fun input → let s = ref input in let words = ref [] in s := dropWhile isSpace !s; while String.length !s != 0 do let (w, s’) = break isSpace !s in words := !words @ [w]; s := s’ done ; !words;; We can program with loops and references in OCaml, but is it elegant? Center for Software Technology Michiel Overeem
    25. Intro to OCaml > First steps in OCaml Counting words (3) We could count the words in OCaml in a different way: words in OCaml - imperative let words’ : string → string list = fun input → let s = ref input in let words = ref [] in s := dropWhile isSpace !s; while String.length !s != 0 do let (w, s’) = break isSpace !s in words := !words @ [w]; s := s’ done ; !words;; We can program with loops and references in OCaml, but is it elegant? Center for Software Technology Michiel Overeem
    26. Intro to OCaml > First steps in OCaml Counting words (3) We could count the words in OCaml in a different way: words in OCaml - imperative let words’ : string → string list = fun input → let s = ref input in let words = ref [] in s := dropWhile isSpace !s; while String.length !s != 0 do let (w, s’) = break isSpace !s in words := !words @ [w]; s := s’ done ; !words;; We can program with loops and references in OCaml, but is it elegant? Center for Software Technology Michiel Overeem
    27. Intro to OCaml > First steps in OCaml Counting words (3) We could count the words in OCaml in a different way: words in OCaml - imperative let words’ : string → string list = fun input → let s = ref input in let words = ref [] in s := dropWhile isSpace !s; while String.length !s != 0 do let (w, s’) = break isSpace !s in words := !words @ [w]; s := s’ done ; !words;; We can program with loops and references in OCaml, but is it elegant? Center for Software Technology Michiel Overeem
    28. Intro to OCaml > First steps in OCaml Counting words (3) We could count the words in OCaml in a different way: words in OCaml - imperative let words’ : string → string list = fun input → let s = ref input in let words = ref [] in s := dropWhile isSpace !s; while String.length !s != 0 do let (w, s’) = break isSpace !s in words := !words @ [w]; s := s’ done ; !words;; We can program with loops and references in OCaml, but is it elegant? Center for Software Technology Michiel Overeem
    29. Intro to OCaml > First steps in OCaml Counting words (3) We could count the words in OCaml in a different way: words in OCaml - imperative let words’ : string → string list = fun input → let s = ref input in let words = ref [] in s := dropWhile isSpace !s; while String.length !s != 0 do let (w, s’) = break isSpace !s in words := !words @ [w]; s := s’ done ; !words;; We can program with loops and references in OCaml, but is it elegant? Center for Software Technology Michiel Overeem
    30. Intro to OCaml > First steps in OCaml Counting the rest We can define lines similar to words. The number of chars can be calculated with let chars s = String.length s ;; Center for Software Technology Michiel Overeem
    31. Intro to OCaml > First steps in OCaml Counting the rest We can define lines similar to words. The number of chars can be calculated with let chars s = String.length s ;; (* We have partial parameterization! *) Center for Software Technology Michiel Overeem
    32. Intro to OCaml > First steps in OCaml lazy vs strict OCaml is strict by default (call-by-value). OCaml can be lazy, by using the keyword lazy . laziness in OCaml let alwaysTwo = 2;; let divideAndPrint i = let print v = print int (alwaysTwo v) in print (lazy (i/0)); print (i/0);; Center for Software Technology Michiel Overeem
    33. Intro to OCaml > First steps in OCaml lazy vs strict OCaml is strict by default (call-by-value). OCaml can be lazy, by using the keyword lazy . laziness in OCaml let alwaysTwo = 2;; let divideAndPrint i = let print v = print int (alwaysTwo v) in print (lazy (i/0)); print (i/0);; 2Fatal error: exception Division_by_zero Center for Software Technology Michiel Overeem
    34. Intro to OCaml > Side-effects Doing some IO We have defined lines, words and chars. How do we read from a file? reading a file let readFile file = let ichannel = open in file in let content = ref ”” in let return = close in ichannel; !content in try while true do content := !content ˆ (input line ichannel) ˆ ”\\n” done ; return () with End of file → return ();; Center for Software Technology Michiel Overeem
    35. Intro to OCaml > Side-effects Doing some IO We have defined lines, words and chars. How do we read from a file? reading a file let readFile file = let ichannel = open in file in let content = ref ”” in let return = close in ichannel; !content in try while true do content := !content ˆ (input line ichannel) ˆ ”\\n” done ; return () with End of file → return ();; Center for Software Technology Michiel Overeem
    36. Intro to OCaml > Side-effects Doing some IO We have defined lines, words and chars. How do we read from a file? reading a file let readFile file = let ichannel = open in file in let content = ref ”” in let return = close in ichannel; !content in try while true do content := !content ˆ (input line ichannel) ˆ ”\\n” done ; return () with End of file → return ();; Center for Software Technology Michiel Overeem
    37. Intro to OCaml > Side-effects Doing some IO We have defined lines, words and chars. How do we read from a file? reading a file let readFile file = let ichannel = open in file in let content = ref ”” in let return = close in ichannel; !content in try while true do content := !content ˆ (input line ichannel) ˆ ”\\n” done ; return () with End of file → return ();; Center for Software Technology Michiel Overeem
    38. Intro to OCaml > Side-effects Doing some IO We have defined lines, words and chars. How do we read from a file? reading a file let readFile file = let ichannel = open in file in let content = ref ”” in let return = close in ichannel; !content in try while true do content := !content ˆ (input line ichannel) ˆ ”\\n” done ; return () with End of file → return ();; Center for Software Technology Michiel Overeem
    39. Intro to OCaml > Side-effects Doing some IO We have defined lines, words and chars. How do we read from a file? reading a file let readFile file = let ichannel = open in file in let content = ref ”” in let return = close in ichannel; !content in try while true do content := !content ˆ (input line ichannel) ˆ ”\\n” done ; return () with End of file → return ();; Center for Software Technology Michiel Overeem
    40. Intro to OCaml > Side-effects Doing some IO We have defined lines, words and chars. How do we read from a file? reading a file let readFile file = let ichannel = open in file in let content = ref ”” in let return = close in ichannel; !content in try while true do content := !content ˆ (input line ichannel) ˆ ”\\n” done ; return () with End of file → return ();; Center for Software Technology Michiel Overeem
    41. Intro to OCaml > Side-effects Doing some IO We have defined lines, words and chars. How do we read from a file? reading a file let readFile file = let ichannel = open in file in let content = ref ”” in let return = close in ichannel; !content in try while true do content := !content ˆ (input line ichannel) ˆ ”\\n” done ; return () with End of file → return ();; Center for Software Technology Michiel Overeem
    42. Intro to OCaml > Side-effects Making our application come together The building blocks for our first OCaml application are ready Processing arguments and printing output let show ls ws cs = print string ( string of int ls ˆ ” ” ˆ string of int ws ˆ ” ” ˆ string of int cs ˆ ”\\n”) let process file = let content = readFile file in show (List.length (lines content)) (List.length (words content)) (String.length content);; let = let argslength = Array.length Sys.argv - 1 in let args = Array.sub Sys.argv 1 argslength in Array.iter process args Center for Software Technology Michiel Overeem
    43. Intro to OCaml > Side-effects Making our application come together The building blocks for our first OCaml application are ready Processing arguments and printing output let show ls ws cs = print string ( string of int ls ˆ ” ” ˆ string of int ws ˆ ” ” ˆ string of int cs ˆ ”\\n”) let process file = let content = readFile file in show (List.length (lines content)) (List.length (words content)) (String.length content);; let = let argslength = Array.length Sys.argv - 1 in let args = Array.sub Sys.argv 1 argslength in Array.iter process args Center for Software Technology Michiel Overeem
    44. Intro to OCaml > Side-effects Making our application come together The building blocks for our first OCaml application are ready Processing arguments and printing output let show ls ws cs = print string ( string of int ls ˆ ” ” ˆ string of int ws ˆ ” ” ˆ string of int cs ˆ ”\\n”) let process file = in acontent =way Printing let different readFile file let show in show (List.length (lines content)) ls ws cs;; cs ws ls = Printf.printf ”%i %i %i\\n” (List.length (words content)) (String.length content);; Printf.printf : (’a, out channel, unit) format → ’a let = let argslength = Array.length Sys.argv - 1 in let args = Array.sub Sys.argv 1 argslength in Array.iter process args Center for Software Technology Michiel Overeem
    45. Intro to OCaml > Side-effects Making our application come together The building blocks for our first OCaml application are ready Processing arguments and printing output let show ls ws cs = print string ( string of int ls ˆ ” ” ˆ string of int ws ˆ ” ” ˆ string of int cs ˆ ”\\n”) let process file = let content = readFile file in show (List.length (lines content)) (List.length (words content)) (String.length content);; let = let argslength = Array.length Sys.argv - 1 in let args = Array.sub Sys.argv 1 argslength in Array.iter process args Center for Software Technology Michiel Overeem
    46. Intro to OCaml > Side-effects Making our application come together The building blocks for our first OCaml application are ready Processing arguments and printing output let show ls ws cs = print string ( string of int ls ˆ ” ” ˆ string of int ws ˆ ” ” ˆ string of int cs ˆ ”\\n”) let process file = let content = readFile file in show (List.length (lines content)) (List.length (words content)) (String.length content);; let = let argslength = Array.length Sys.argv - 1 in let args = Array.sub Sys.argv 1 argslength in Array.iter process args Center for Software Technology Michiel Overeem
    47. Intro to OCaml > Side-effects Making our application come together The building blocks for our first OCaml application are ready Processing arguments and printing output let show ls ws cs = print string ( string of int ls ˆ ” ” ˆ string of int ws ˆ ” ” ˆ string of int cs ˆ ”\\n”) let process file = let content = readFile file in show (List.length (lines content)) (List.length (words content)) (String.length content);; let = let argslength = Array.length Sys.argv - 1 in let args = Array.sub Sys.argv 1 argslength in Array.iter process args Center for Software Technology Michiel Overeem
    48. Intro to OCaml > Side-effects Making our application come together The building blocks for our first OCaml application are ready Processing arguments and printing output Arrays in Ocaml let show ls ws OCaml are string ( string of int destructive updates. Arrays in cs = print mutable, they have ls ˆ ” ” ˆ string of int ws ˆ ” ” ˆ let show content = let statsstring of int cs ˆ ”\\n”) = Array.make 3 1 in stats.(0) ← List.length (lines content); let process file = let contentstats.(1) ← file = readFile List.length (words content); in show (List.length ← String.length content; stats.(2) (lines content)) (List.length ( fun → print Array.iter (wordsx content))int x; (String.length print string ” ”) stats content);; let = let argslength = Array.length Sys.argv - 1 in let args = Array.sub Sys.argv 1 argslength in Array.iter process args Center for Software Technology Michiel Overeem
    49. Intro to OCaml > Side-effects Making our application come together The building blocks for our first OCaml application are ready Processing arguments and printing output Arrays in Ocaml let show ls ws OCaml are string ( string of int destructive updates. Arrays in cs = print mutable, they have ls ˆ ” ” ˆ string of int ws ˆ ” ” ˆ let show content = let statsstring of int cs ˆ ”\\n”) = Array.make 3 1 in stats.(0) ← List.length (lines content); let process file = let contentstats.(1) ← file = readFile List.length (words content); in show (List.length ← String.length content; stats.(2) (lines content)) (List.length ( fun → print Array.iter (wordsx content))int x; (String.length print string ” ”) stats content);; let = let argslength = Array.length Sys.argv - 1 in let args = Array.sub Sys.argv 1 argslength in Array.iter process args Center for Software Technology Michiel Overeem
    50. Intro to OCaml > Side-effects Making our application come together The building blocks for our first OCaml application are ready Processing arguments and printing output Arrays in Ocaml let show ls ws OCaml are string ( string of int destructive updates. Arrays in cs = print mutable, they have ls ˆ ” ” ˆ string of int ws ˆ ” ” ˆ let show content = let statsstring of int cs ˆ ”\\n”) = Array.make 3 1 in stats.(0) ← List.length (lines content); let process file = let contentstats.(1) ← file = readFile List.length (words content); in show (List.length ← String.length content; stats.(2) (lines content)) (List.length ( fun → print Array.iter (wordsx content))int x; (String.length print string ” ”) stats content);; let = let argslength = Array.length Sys.argv - 1 in let args = Array.sub Sys.argv 1 argslength in Array.iter process args Center for Software Technology Michiel Overeem
    51. Intro to OCaml > Side-effects Making our application come together The building blocks for our first OCaml application are ready Processing arguments and printing output Arrays in Ocaml let show ls ws OCaml are string ( string of int destructive updates. Arrays in cs = print mutable, they have ls ˆ ” ” ˆ string of int ws ˆ ” ” ˆ let show content = let statsstring of int cs ˆ ”\\n”) = Array.make 3 1 in stats.(0) ← List.length (lines content); let process file = let contentstats.(1) ← file = readFile List.length (words content); in show (List.length ← String.length content; stats.(2) (lines content)) Array.iter ( fun → print (List.length (wordsx content))int x; (String.length print string ” ”) stats content);; let = let argslength = Array.length Sys.argv - 1 in let args = Array.sub Sys.argv 1 argslength in Array.iter process args Center for Software Technology Michiel Overeem
    52. Intro to OCaml > Side-effects Making our application come together The building blocks for our first OCaml application are ready Processing arguments and printing output let show ls ws cs = print string ( string of int ls ˆ ” ” ˆ string of int ws ˆ ” ” ˆ string of int cs ˆ ”\\n”) let process file = let content = readFile file in show (List.length (lines content)) (List.length (words content)) (String.length content);; let = let argslength = Array.length Sys.argv - 1 in let args = Array.sub Sys.argv 1 argslength in Array.iter process args Center for Software Technology Michiel Overeem
    53. Intro to OCaml > Side-effects Making our application come together The building blocks for our first OCaml application are ready Processing arguments and printing output let show ls ws cs = print string ( string of int ls ˆ ” ” ˆ string of int ws ˆ ” ” ˆ string of int cs ˆ ”\\n”) Processing our arguments (2) let process file = 1let content = readFile file- 1 do for i to Array.length Sys.argv in show (List.length (lines content)) process Sys.argv.(i) (List.length (words content)) done ;; (String.length content);; let = let argslength = Array.length Sys.argv - 1 in let args = Array.sub Sys.argv 1 argslength in Array.iter process args Center for Software Technology Michiel Overeem
    54. Intro to OCaml > The O in OCaml Counting words in object-oriented style The O stands for Objective: we can use classes and objects in OCaml. class wcCounter file = let content = readFile file in object (self) method private words = . . . method show = . . . end ;; for i = 1 to Array.length Sys.argv - 1 do let counter = new wcCounter Sys.argv.(i) in counter#show done ;; Center for Software Technology Michiel Overeem
    55. Intro to OCaml > The O in OCaml Counting words in object-oriented style The O stands for Objective: we can use classes and objects in OCaml. class wcCounter file = let content = readFile file in object (self) method private words = . . . method show = . . . end ;; for i = 1 to Array.length Sys.argv - 1 do let counter = new wcCounter Sys.argv.(i) in counter#show done ;; Center for Software Technology Michiel Overeem
    56. Intro to OCaml > The O in OCaml Counting words in object-oriented style The O stands for Objective: we can use classes and objects in OCaml. class wcCounter file = let content = readFile file in object (self) method private words = . . . method show = . . . end ;; for i = 1 to Array.length Sys.argv - 1 do let counter = new wcCounter Sys.argv.(i) in counter#show done ;; Center for Software Technology Michiel Overeem
    57. Intro to OCaml > The O in OCaml Counting words in object-oriented style The O stands for Objective: we can use classes and objects in OCaml. class wcCounter file = let content = readFile file in object (self) method private words = . . . method show = . . . end ;; for i = 1 to Array.length Sys.argv - 1 do let counter = new wcCounter Sys.argv.(i) in counter#show done ;; Center for Software Technology Michiel Overeem
    58. Intro to OCaml > The O in OCaml Counting words in object-oriented style The O stands for Objective: we can use classes and objects in OCaml. class wcCounter file = let content = readFile file in object (self) method private words = . . . method show = . . . end ;; for i = 1 to Array.length Sys.argv - 1 do let counter = new wcCounter Sys.argv.(i) in counter#show done ;; Center for Software Technology Michiel Overeem
    59. Intro to OCaml > The O in OCaml Counting words in object-oriented style The O stands for Objective: we can use classes and objects in OCaml. class wcCounter file = let content = readFile file in object (self) method private words = . . . method show = . . . end ;; for i = 1 to Array.length Sys.argv - 1 do let counter = new wcCounter Sys.argv.(i) in counter#show done ;; Center for Software Technology Michiel Overeem
    60. Intro to OCaml > The O in OCaml Counting words in object-oriented style The O stands for Objective: we can use classes and objects in OCaml. class wcCounter file = let content = readFile file in object (self) method private words = . . . method show = . . . end ;; for i = 1 to Array.length Sys.argv - 1 do let counter = new wcCounter Sys.argv.(i) in counter#show done ;; Center for Software Technology Michiel Overeem
    61. Intro to OCaml > The O in OCaml Counting words in object-oriented style The O stands for Objective: we can use classes and objects in OCaml. class wcCounter file = let content = readFile file in object (self) method private words = . . . method show = . . . end ;; for i = 1 to Array.length Sys.argv - 1 do let counter = new wcCounter Sys.argv.(i) in counter#show done ;; Center for Software Technology Michiel Overeem
    62. Intro to OCaml > The O in OCaml Objects vs Datatypes Object features: inheritance encapsulation (private members) constructor function (for initialization) Datatype features: pattern matching multiple type constructors Center for Software Technology Michiel Overeem
    63. Intro to OCaml > Large applications Modularize your application Until now we have only seen a small application. How do we structure our larger applications? module Set = struct type element = string type set = element list let rec member x s = match s with ... end ;; Center for Software Technology Michiel Overeem
    64. Intro to OCaml > Large applications Modularize your application Until now we have only seen a small application. How do we structure our larger applications? module Set = struct type element = string type set = element list let rec member x s = match s with ... end ;; Center for Software Technology Michiel Overeem
    65. Intro to OCaml > Large applications Modularize your application Until now we have only seen a small application. How do we structure our larger applications? module Set = struct type element = string type set = element list let rec member x s = match s with ... end ;; Center for Software Technology Michiel Overeem
    66. Intro to OCaml > Large applications Modularize your application Until now we have only seen a small application. How do we structure our larger applications? module Set = struct type element = string type set = element list let rec member x s = match s with ... end ;; Center for Software Technology Michiel Overeem
    67. Intro to OCaml > Large applications Parameterize your module Now we have a Set module, but with a fixed type and comparison function. We want this to be parameterized! Center for Software Technology Michiel Overeem
    68. Intro to OCaml > Large applications Parameterize your module Now we have a Set module, but with a fixed type and comparison function. We want this to be parameterized! We first define a module type for comparison. type comparison = LT | GT | EQ;; module type OrderedType = sig type t val compare : t → t → comparison end ;; Center for Software Technology Michiel Overeem
    69. Intro to OCaml > Large applications Parameterize your module (2) We can now define a default ordering on strings: module OrderedString : OrderedType = struct type t = string let compare x y = . . . end ;; Center for Software Technology Michiel Overeem
    70. Intro to OCaml > Large applications Parameterize your module (2) We can now define a default ordering on strings: module OrderedString : OrderedType = struct type t = string let compare x y = . . . end ;; But we might want a second, case insensitive, ordering: module CIOrderedString : OrderedType = struct type t = string let compare x y = let x’ = String.uppercase x in let . . . Center for Software Technology Michiel Overeem
    71. Intro to OCaml > Large applications Parameterize your module (3) How do we give a specific comparison to the Set module? module Set (Ord : OrderedType) = struct type element = Ord.t type set = element list ... let rec member x s in match s with → false [] | hd::tl → match Ord.compare x hd with ... end ;; Center for Software Technology Michiel Overeem
    72. Intro to OCaml > Large applications Parameterize your module (3) How do we give a specific comparison to the Set module? module Set (Ord : OrderedType) = struct type element = Ord.t type set = element list ... let rec member x s in match s with → false [] | hd::tl → match Ord.compare x hd with ... end ;; Center for Software Technology Michiel Overeem
    73. Intro to OCaml > Large applications Parameterize your module (3) How do we give a specific comparison to the Set module? module Set (Ord : OrderedType) = struct type element = Ord.t type set = element list ... let rec member x s in match s with → false [] | hd::tl → match Ord.compare x hd with ... end ;; Center for Software Technology Michiel Overeem
    74. Intro to OCaml > Large applications Parameterize your module (4) Now we can define our specific modules: module StringSet = Set (OrderedString);; module CIStringSet = Set (CIOrderedString);; And we could parameterize even further . . . Center for Software Technology Michiel Overeem
    75. Intro to OCaml > Large applications Parameterize your module (4) Now we can define our specific modules: module StringSet = Set (OrderedString);; module CIStringSet = Set (CIOrderedString);; And we could parameterize even further . . . The OCaml modules are not first class, because they are units of compilation. They do offer something different from Haskell’s type classes: we can define different comparisons on the same type in the same scope. Center for Software Technology Michiel Overeem
    76. Intro to OCaml > Some last remarks More features We have seen some of the most exciting features of OCaml, but there is much more: Records There are bindings to GTK+ and wxWidgets Syntax extensions for defining DSL’s Lot’s of (standard) libraries Center for Software Technology Michiel Overeem
    77. Intro to OCaml > Some last remarks Finally, the performance People tell that OCaml is fast, way faster than Haskell. (see The great computer language shootout) The reason for OCaml’s excellent performance: strict evaluation the compiler mutable datastructures Or as some would say trading elegance for efficiency. Center for Software Technology Michiel Overeem
    78. Intro to OCaml > Some last remarks Current research on OCaml AtomCaml This project gives the ability to make execution of code atomic. Aspectual Caml Brings aspect-oriented features to the OCaml langague. F# An OCaml-like language on the .NET platform. Center for Software Technology Michiel Overeem
    79. Intro to OCaml > Conclusion My thoughts What I like about OCaml: it’s functional and object-oriented the module system is powerful it’s fast without weird hacks (they say) Center for Software Technology Michiel Overeem
    80. Intro to OCaml > Conclusion My thoughts What I like about OCaml: it’s functional and object-oriented the module system is powerful it’s fast without weird hacks (they say) What I do not like it’s functional and object-oriented verbose syntax (top level let and ; ; ) sometimes efficiency over elegance Center for Software Technology Michiel Overeem

    + michielovereemmichielovereem, 6 months ago

    custom

    836 views, 1 favs, 0 embeds more stats

    Introducing OCaml to students familiar with functio more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 836
      • 836 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 0
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories