

So this immediately returns s, and the function is a period (refer to the original string, We go through the same dance for i = 1 and end up calling loop 2.įor i = 2, however, s.

I th character in s) is not a period or 'e'. I is not greater than or equal to l, and s. Suppose the function is called with f =ġ2.34, then s = "12.34" and l = 5. = 'e' then s else loop ( i + 1 ) in loop 0 Pair of nested if expressions: let string_of_float f = let s = format_float " %.12g " f in let l = string_length s in let rec loop i = if i >= l then s ^ ". The abs (absolute value) function is defined in Stdlib as: # let abs x = if x >= 0 then x else - x val abs : int -> int = Īlso in Stdlib, the string_of_float function contains a complex What does this function do? # let f x y = x + if y > 0 then y else 0 val f : int -> int -> int = Ĭlue: add brackets around the whole of the if expression. We'll discuss recursion more at the end of thisīack, temporarily, to if statements. Programming can be said to prefer recursion over loops, but I'm jumpingĪhead of myself. What we've got here is a simple case of recursion. It should be fairly clear that range 1 10 evaluates to Our formal description of lists and the :: (cons) operator? 10 :: Worked out that range 11 10 =, so this is: 10. The brackets to make the order of evaluation more clear). What about calling range 10 10? Since 10 > 10 is false, theĮlse-clause is evaluated, which is: 10 :: (range 11 10) (I've added Let's examine some typical calls to this function, starting with theĮasy case of a > b. You shouldīe able to combine your knowledge of recursive functions, lists and ifĮxpressions to see what it does: # let rec range a b = if a > b then else a : : range ( a + 1 ) b val range : int -> int -> int list = Here's the rangeįunction which I showed you earlier without much explanation. Let's look a bit more closely at the if expression. This is because > is in fact polymorphic. Notice that OCaml decides that this function is polymorphic, with theįollowing type: # max - : 'a -> 'a -> 'a = Īnd indeed OCaml lets you use max on any type: # max 3 5 - : int = 5 # max 3.5 13.0 - : float = 13. Here's a simple example of an if statement in OCaml: # let max a b = if a > b then a else b val max : 'a -> 'a -> 'a = Īs a short aside, if you type this into the OCaml In other words, they're much more likeīoolean-condition ? expression : other-expression in C than like its if

Unlike conventional languages, OCaml if statementsĪre really expressions. OCaml has an if statement with two variations: if boolean - condition then expression if boolean - condition then expression else other - expression
#JAVA FOR LOOP SHORTHAND HOW TO#
It's an example of how to write the code.
#JAVA FOR LOOP SHORTHAND CODE#
If the code doesn't start with # and end in , Code snippets that begin with the CLI prompt #, end with, and have aĬlear output can be tested in the OCaml toplevel ( ocaml or utop) As in other documentation, the code examples will either be something you can test orĪn example of code.
