Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml: Does storing some values to be used later introduce "side effects"?

For a homework assignment, we've been instructed to complete a task without introducing any "side-effects". I've looked up "side-effects" on Wikipedia, and though I get that in theory it means "modifies a state or has an observable interaction with calling functions", I'm having trouble figuring out specifics.

For example, would creating a value that holds a non-compile time result be introducing side effects?

Say I had (might not be syntactically perfect):

val myList = (someFunction x y);;
if List.exists ((=) 7) myList then true else false;;

Would this introduce side-effects? I guess maybe I'm confused on what "modifies a state" means in the definition of side-effects.

like image 874
Casey Patton Avatar asked Sep 28 '11 05:09

Casey Patton


People also ask

What is side effect in OCaml?

Pure and impure functional programming A pure function is one without any side-effects. A side-effect really means that the function keeps some sort of hidden state inside it. strlen is a good example of a pure function in C. If you call strlen with the same string, it always returns the same length.

What is imperative OCaml?

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).

What is let in OCaml?

the let expression is the main way to bind a name to a value. since functions are values just like ints or strings, let is also used to define functions. to define simple values: let name = value.

What keyword is used to declare variables and functions in OCaml?

At its simplest, a variable is an identifier whose meaning is bound to a particular value. In OCaml these bindings are often introduced using the let keyword.


1 Answers

No; a side-effect refers to e.g. mutating a ref cell with the assignment operator :=, or other things where the value referred to by a name changes over time. In this case, myList is an immutable value that never changes during the program, thus it is effect-free.

See also

http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)

like image 153
Brian Avatar answered Sep 25 '22 20:09

Brian