putting the o in ocaml
play

Putting the O in OCaml CS 51 and CSCI E-51 March 27, 2014 To - PowerPoint PPT Presentation

Putting the O in OCaml CS 51 and CSCI E-51 March 27, 2014 To install the graphics library on your appliance: . curl -sL http://goo.gl/f8J4L6 | sh But first, a word about final projects Requirements: Teams of 2 to 4 Any language


  1. Putting the “O” in OCaml CS 51 and CSCI E-51 March 27, 2014 To install the graphics library on your appliance: . curl -sL http://goo.gl/f8J4L6 | sh

  2. But first, a word about final projects Requirements: • Teams of 2 to 4 • Any language you please • Must be computationally interesting • Proposals due 4/4 (a week from tomorrow) • More details will be forthcoming… 2:1

  3. Some example projects (Java) optimum play for a strategy game (Java) • A document search suite and plagarism detector (OCaml) • A version control system, focusing on the rsync and compression algorithms • Machine learning for games: genetic algorithm & neural network to find • A simplex solver with a complete matrix module (OCaml) • Collaborative filtering using five algorithms (OCaml) • Mini-WolframAlpha (OCaml) • Comparison of dictionary performance with 6 different data structures (Java) • SAT solver (OCaml) • Content-aware image resizing (Python) • Natural language parsing (Python) 3:1

  4. type display_elt = Rectangle of rect | Circle of circ | Text of text type scene = display_elt list A crude GUI type point = { x : int; y : int } type rect = { rect_ll : point; rect_width : int; rect_height : int } type circ = { circ_center : point; circ_radius : int } type text = { text_ll : point; text_value : string } 4:1

  5. type scene = display_elt list A crude GUI type point = { x : int; y : int } type rect = { rect_ll : point; rect_width : int; rect_height : int } type circ = { circ_center : point; circ_radius : int } type text = { text_ll : point; text_value : string } type display_elt = Rectangle of rect | Circle of circ | Text of text 4:2

  6. A crude GUI type point = { x : int; y : int } type rect = { rect_ll : point; rect_width : int; rect_height : int } type circ = { circ_center : point; circ_radius : int } type text = { text_ll : point; text_value : string } type display_elt = Rectangle of rect | Circle of circ | Text of text type scene = display_elt list 4:3

  7. let draw_scene : scene -> unit = List.iter ~f:draw Rendering . . . . let draw (elt : display_elt) : unit = match elt with | Rectangle r -> fill_rect r.rect_ll.x r.rect_ll.y r.rect_width r.rect_height | Circle c -> fill_circle c.circ_center.x c.circ_center.y c.circ_radius | Text t -> moveto t.text_ll.x t.text_ll.y; draw_string t.text_value 5:1

  8. let draw_scene : scene -> unit = List.iter ~f:draw Rendering . . . . . . let draw (elt : display_elt) : unit = match elt with | Rectangle r -> fill_rect r.rect_ll.x r.rect_ll.y r.rect_width r.rect_height | Circle c -> fill_circle c.circ_center.x c.circ_center.y c.circ_radius | Text t -> moveto t.text_ll.x t.text_ll.y; draw_string t.text_value 5:2

  9. let draw_scene : scene -> unit = List.iter ~f:draw Rendering . . . . . . let draw (elt : display_elt) : unit = match elt with | Rectangle r -> fill_rect r.rect_ll.x r.rect_ll.y r.rect_width r.rect_height | Circle c -> fill_circle c.circ_center.x c.circ_center.y c.circ_radius | Text t -> moveto t.text_ll.x t.text_ll.y; draw_string t.text_value 5:3

  10. let draw_scene : scene -> unit = List.iter ~f:draw Rendering . . . . . . let draw (elt : display_elt) : unit = match elt with | Rectangle r -> fill_rect r.rect_ll.x r.rect_ll.y r.rect_width r.rect_height | Circle c -> fill_circle c.circ_center.x c.circ_center.y c.circ_radius | Text t -> moveto t.text_ll.x t.text_ll.y; draw_string t.text_value 5:4

  11. let draw_scene : scene -> unit = List.iter ~f:draw Rendering . . . . . . let draw (elt : display_elt) : unit = match elt with | Rectangle r -> fill_rect r.rect_ll.x r.rect_ll.y r.rect_width r.rect_height | Circle c -> fill_circle c.circ_center.x c.circ_center.y c.circ_radius | Text t -> moveto t.text_ll.x t.text_ll.y; draw_string t.text_value 5:5

  12. Rendering . . . . let draw (elt : display_elt) : unit = match elt with | Rectangle r -> fill_rect r.rect_ll.x r.rect_ll.y r.rect_width r.rect_height | Circle c -> fill_circle c.circ_center.x c.circ_center.y c.circ_radius | Text t -> moveto t.text_ll.x t.text_ll.y; draw_string t.text_value let draw_scene : scene -> unit = List.iter ~f:draw 5:6

  13. — To Emacs —

  14. let rect (ll : point) (width : int) (height : int) : display_elt = { draw = fun () -> fill_rect ll.x ll.y width height } let circ (center : point) (radius : int) : display_elt = { draw = fun () -> fill_circle center.x center.y radius } let text (ll : point) (value : string) : display_elt = { draw = fun () -> moveto ll.x ll.y; draw_string value } type scene = display_elt list A different approach type display_elt = { draw : unit -> unit } 7:1

  15. let circ (center : point) (radius : int) : display_elt = { draw = fun () -> fill_circle center.x center.y radius } let text (ll : point) (value : string) : display_elt = { draw = fun () -> moveto ll.x ll.y; draw_string value } type scene = display_elt list A different approach type display_elt = { draw : unit -> unit } let rect (ll : point) (width : int) (height : int) : display_elt = { draw = fun () -> fill_rect ll.x ll.y width height } 7:2

  16. let text (ll : point) (value : string) : display_elt = { draw = fun () -> moveto ll.x ll.y; draw_string value } type scene = display_elt list A different approach type display_elt = { draw : unit -> unit } let rect (ll : point) (width : int) (height : int) : display_elt = { draw = fun () -> fill_rect ll.x ll.y width height } let circ (center : point) (radius : int) : display_elt = { draw = fun () -> fill_circle center.x center.y radius } 7:3

  17. type scene = display_elt list A different approach type display_elt = { draw : unit -> unit } let rect (ll : point) (width : int) (height : int) : display_elt = { draw = fun () -> fill_rect ll.x ll.y width height } let circ (center : point) (radius : int) : display_elt = { draw = fun () -> fill_circle center.x center.y radius } let text (ll : point) (value : string) : display_elt = { draw = fun () -> moveto ll.x ll.y; draw_string value } 7:4

  18. A different approach type display_elt = { draw : unit -> unit } let rect (ll : point) (width : int) (height : int) : display_elt = { draw = fun () -> fill_rect ll.x ll.y width height } let circ (center : point) (radius : int) : display_elt = { draw = fun () -> fill_circle center.x center.y radius } let text (ll : point) (value : string) : display_elt = { draw = fun () -> moveto ll.x ll.y; draw_string value } type scene = display_elt list 7:5

  19. — To Emacs —

  20. This is OO Interfaces An OCaml record type Instance variables are hidden All interaction is via methods Extensible types Easy to add new kinds of display elements Each object defines its own behavior But not so easy to add new methods… • Objects ▶ Represented as OCaml record ▶ Collection of named values— instance variables ▶ Collection of operations— methods 9:1

  21. This is OO Extensible types Easy to add new kinds of display elements Each object defines its own behavior But not so easy to add new methods… • Objects ▶ Represented as OCaml record ▶ Collection of named values— instance variables ▶ Collection of operations— methods • Interfaces ▶ An OCaml record type ▶ Instance variables are hidden ▶ All interaction is via methods 9:2

  22. But not so easy to add new methods… This is OO • Objects ▶ Represented as OCaml record ▶ Collection of named values— instance variables ▶ Collection of operations— methods • Interfaces ▶ An OCaml record type ▶ Instance variables are hidden ▶ All interaction is via methods • Extensible types ▶ Easy to add new kinds of display elements ▶ Each object defines its own behavior 9:3

  23. This is OO • Objects ▶ Represented as OCaml record ▶ Collection of named values— instance variables ▶ Collection of operations— methods • Interfaces ▶ An OCaml record type ▶ Instance variables are hidden ▶ All interaction is via methods • Extensible types ▶ Easy to add new kinds of display elements ▶ Each object defines its own behavior ▶ But not so easy to add new methods… 9:4

  24. Datatypes versus objects (production) (consumption) (production) (consumption) • Datatypes are closed ▶ Hard to add new alternatives ▶ Easy to add new operations • Objects are open ▶ Easy to add new alternatives ▶ Hard to add new operations 10:1

  25. Datatypes versus objects • Datatypes are closed ▶ Hard to add new alternatives (production) ▶ Easy to add new operations (consumption) • Objects are open ▶ Easy to add new alternatives (production) ▶ Hard to add new operations (consumption) 10:2

  26. Other OO concepts Inheritance (implementation reuse) Base new classes on existing ones Can change or extend behavior Subtyping (is not inheritance!) Use an object where a smaller interface is expected C++, Java, and C# confuse subtyping with inheritance • Classes ▶ Specify how to build objects ▶ Like our functions rect , circ , etc. 11:1

  27. Other OO concepts Subtyping (is not inheritance!) Use an object where a smaller interface is expected C++, Java, and C# confuse subtyping with inheritance • Classes ▶ Specify how to build objects ▶ Like our functions rect , circ , etc. • Inheritance (implementation reuse) ▶ Base new classes on existing ones ▶ Can change or extend behavior 11:2

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend