lec12.pdf - lec12(Side Effects CS3100 Fall 2019 Why Side Effects We have only used purely functional feature of OCaml Our study of lambda calculus

lec12.pdf - lec12(Side Effects CS3100 Fall 2019 Why Side...

This preview shows page 1 - 5 out of 12 pages.

05/09/2019 lec12 127.0.0.1:8888/notebooks/lec12/lec12.ipynb 1/12 (Side) Effects CS3100 Fall 2019 Why Side Effects We have only used purely functional feature of OCaml Our study of lambda calculus used only purely functional features The above statements are lies . We have used print_endline , printf and other features to display our results to screen. It is sometimes useful to write programs that have side effects Side effects Side effects include Mutating (i.e., destructively updating) the values of program state. Reading from standard input, printing to standard output. Reading and writing to files, sockets, pipes etc. ... Composing, sending and receiving emails, editing documents, writing this slide, etc. Side effects in OCaml OCaml programs can include side effects Features Mutations: Reference cells, Arrays, Mutable record fields I/O of all sorts In this lecture, Mutations Reference cells
Image of page 1
05/09/2019 lec12 127.0.0.1:8888/notebooks/lec12/lec12.ipynb 2/12 Aka "refs" or "ref cell" Pointer to a typed location in memory The binding of a variable to a ref cell is but the contents of the ref cell may . x r o1 o2 variable ref cell 200 1 1   A Reference cells In [ ]: In [ ]: Reference Cells: Types In [ ]: In [ ]: In [ ]: let r = ref 0 r := ! r + 1 ; ! r ref ( ! ) ( := )
Image of page 2
05/09/2019 lec12 127.0.0.1:8888/notebooks/lec12/lec12.ipynb 3/12 Implementing a counter In [ ]: In [ ]: In [ ]: Side effects make reasoning hard Recall that referential transparency allows replacing with if . Side effects break referential transparency. Referential transparency Consider the function foo : In [ ]: In [ ]: baz can now be optimised to In [ ]: Referential transparency let make_counter init = let c = ref init in fun () -> (c := ! c + 1 ; ! c) let next = make_counter 0 next() let foo x = x + 1 let baz = foo 10 let baz = 11
Image of page 3
05/09/2019 lec12 127.0.0.1:8888/notebooks/lec12/lec12.ipynb
Image of page 4
Image of page 5

You've reached the end of your free preview.

Want to read all 12 pages?

  • Fall '19

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors