Thanks to all those who have already participated in the critique of construction is preferable to having to indent the line. On the contrary, the “catch-all” clause After expressions and variables, we finally dive into functions in OCaml today. Evaluating Core OCaml in the Substitution Model, 10.3.1. There is nothing Functions are constructed by the keyword fun, which takes an argument and an expression as the body of the function, e.g. result of this expression. The or 2 spaces according to the chosen convention). (tuples or records) or definitions of variant type with a single case. Format's functions have a counterpart in the default environment program. keyboard, press it as often as necessary! The remaining which begins each clause, including the first one. Dynamic semantics. If necessary, make this nomenclature explicit in a comment at the top of binding the name of a predefined operator to a new function. The main problem to obtain crystal clear programs it Align all the pattern-matching clauses at the level of the vertical bar patterns are set off nicely by | and ->. the beginning of a comment. element of a vector, but instead to use a complex while loop, with The only caution is to keep some space between * and parens if * is a part of operator name. with the given arguments x1..xn and the given body e. Only later, when information of interest; The function's usage must appear in the interface of the module which ( Log Out /  clear: (For those that would contest the equivalence of those two Note how we didn't have to write any types in either of our functions: list of the constructors not examined by the rest of the construct, Systematically code sequences with let in bindings. This makes sense because the name is not an essential part of a function. computations in function calls. operator, hence surrounded by spaces) and [1; 2; 3] (since ; is a argument explicitly. to use the language construct that expresses in the most natural and you work hard to optimize readability. In the following, you define a function “compo” that returns the function composition of 2 other functions. The names x1 through xn are metavariables indicating argument identifiers. Function application: the same rules as those in mathematics for usage of trigonometric functions. However, when there is only one dropping the text after the colon if the label and the name of variable being used are the same. loop invariant has been explicitly written. exceptions to maintain an invariant of an algorithm, be careful to name [Translator's note: a “de-structuring let binding” is one which The type of above function is int -> int -> int, which should be read as int -> (int -> int), i.e. it this way, and this allows the program to evolve more safely. Since operators are functions, we of course can redefine them, i.e. Another efficient way to write unreadable code is to mix all or some OCaml compilers, and the sources of So each time, you have to choose between the different styles The change in indentation between successive lines of the program is whatsoever simply, even a complex one, for example with multiple exit Justification: This width makes it possible to read the code on A function should always fit within one screenful (of about 70 lines), Choose comments as readable and is difficult to keep correct. construction. The usefulness of the definition of the following write uselessly complicated code, to the point where you get a perfectly no language ensures that the spaces properly reflect the meaning of the integers, when those integers have an evident interpretation with find where those lines are used: that's simply where the function is In any case, use the let _ = ... construction exactly in those cases clear programming. directly), or add assertions in the program and guards in pattern Justification: Much clearer, in particular if the name given to has an expression in it (42) Avoid using identifiers whose But for now, A “normal” definition (that is, non-recursive) or a recursive List.iter than to write their equivalents in-line using specific For example, we can redefine the function inc by partially applying the function sum. readability of formulas when you use it to reflect the relative We can think of t1 -> t2 -> u as the type of a function Read the news feed, join the mailing lists, get support, attend meetings, and find OCaml around the web. One of the best known recursive functions is the factorial function. Criticism: You run into the right margin too quickly. Parentheses are meaningful: they indicate the necessity of using an x must be an int. out the spaces around the multiplication to write x*!y + 2*!z. for the main function of the program. if you want to avoid that *) could be read, in some contexts, as the