rec11 - CS 3110 Recitation 11 Refs and Arrays Warning: You...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 3110 Recitation 11 Refs and Arrays Warning: You should NOT be using any of the features described in this recitation for your implementation of PS3. Any implementation of PS3 using imperative features of OCaml such as refs or arrays will get a zero. Thus far, we've been working with the purely functional fragment of OCaml. That is, we've been working with the subset of the language that does not include computational effects (also known as side effects) other than printing. In particular, whenever we coded a function, we never changed variables or data. Rather, we always computed new data. For instance, when we wrote code for an abstract data type such as a stack, queue, or dictionary, the operations to insert an item into the data structure didn't effect the old copy of the data structure. Instead, we always built a new data structure with the item appropriately inserted. (Note that the new data structure might refer to the old data structure, so this isn't as inefficient as it first sounds.) For the most part, coding in a functional style (i.e., without side effects) is a "good thing" because it's easier to reason locally about the behavior of the code. For instance, when we code purely functional queues or stacks, we don't have to worry about a non-local change to a queue or stack. However, in some situations, it is more efficient or clearer to destructively modify a data structure than to build a new version. In these situations, we need some form of mutable data structures. Like most imperative programming languages, OCaml provides support for mutable data structures, but unlike languages such as C, C++, or Java, they are not the default. Thus, programmers are encouraged to code purely functionally by default and to only resort to mutable data structures when absolutely necessary. In addition, unlike imperative languages, OCaml provides no support for mutable variables. In other words, the value of a variable cannot change in OCaml. Rather, all mutations must occur through data structures. Refs There are only two built-in mutable data structures in OCaml: refs and arrays. OCaml supports imperative programming through the primitive parameterized ref type. A value of type "int ref" is a pointer to a location in memory, where the location in memory contains an integer. It's analogous to "int*" in C/C++ or "Integer" in Java (but not "int" in Java). Like lists, refs are polymorphic, so in fact, we can have a ref (i.e., pointer) to a value of any type. A partial signature for refs is below:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

rec11 - CS 3110 Recitation 11 Refs and Arrays Warning: You...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online