OCaml: retain value of variable with control statements -
i'm new ocaml / functional programming, , i'm confused implementation of things relatively simple other languages know. use , help.
chiefly: in program i'm working on, either increment or decrement variable based on parameter. here's representative of have:
let tot = ref 0 in = 0 s if test_num > 0 tot := !tot + other_num else tot := !tot - other_num done;;
this not way go it, because if else
statement never taken, code acts if is, each , every time, presumably because it's closer bottom of program? know ocaml has pretty sophisticated pattern matching, within level of coed need access handful of lists i've created, and, far understand, can't access lists top-level function without passing them parameters.
i know i'm going wrong way, have no idea how idiomatically.
suggestions? thanks.
edit here's more concise example:
let ex_list = [1; -2; 3; -4] in let max_mem = ref 0 in let mem = ref 0 in let () = = 0 3 let transition = list.nth ex_list in if transition > 0 ( mem := (!mem + 10); ) else mem := (!mem - 1); if (!mem > !max_mem) (max_mem := !mem); done; print_int !max_mem; print_string "\n"; in !mem;
at end, when print max_mem
, 19, though value should (0 + 10 - 1 + 10 - 1 = 18). doing math wrong, or problem come somewhere else?
your code looks fine me. doesn't make lot of sense actual code, think you're trying show general layout. it's written in imperative style, try avoid if possible.
the if
in ocaml acts in other languages, there's no special thing being near bottom of program. (more precisely, acts ? :
ternary operator c , related languages; i.e., it's expression.)
your code doesn't return useful value; returns ()
(the quintessentially uninteresting value known "unit").
if replace free variables (ones not defined in bit of code) constants, , change code return value, can run it:
# let s = 8 in let other_num = 7 in let test_num = 3 in let tot = ref 0 in let () = = 0 s if test_num > 0 tot := !tot + other_num else tot := !tot - other_num done in !tot;; - : int = 63 #
if you're trying learn write in functional style (i.e., without mutable variables), write loop recursive function , make tot
parameter:
# let s = 8 in let other_num = 7 in let test_num = 3 in let rec loop n tot = if n > s tot else let tot' = if test_num > 0 tot + other_num else tot - other_num in loop (n + 1) tot' in loop 0 0;; - : int = 63
it easier if gave (edited add: small :-) self-contained problem you're trying solve.
the other parts of question aren't clear enough give advice on. 1 thing might point out it's idiomatic use pattern matching when processing lists.
also, there's nothing wrong passing things parameters. that's why language called "functional" -- code consists of functions, have parameters.
update
i write let () = expr1 in expr2
instead of expr1; expr2
. it's habit got into, sorry if it's confusing. essence you're evaluating first expression side effects (it has type unit
), , returning value of second expression.
if don't have after for
, code evaluate ()
, said. since purpose of code seems to compute value of !tot
, returned. @ least, lets see calculated value in ocaml top level.
tot'
variable. if calculate new value straightforwardly variable named var
, it's conventional name new value var'
. reads "var prime".
update 2
your example code works ok, has problem uses list.nth
traverse list, slow (quadratic) operation. in fact code naturally considered fold. here's how might write in functional style:
# let ex_list = [1; -2; 3; -4] in let process (tot, maxtot) transition = let tot' = if transition > 0 tot + 10 else tot - 1 in (tot', max maxtot tot') in list.fold_left process (0, 0) ex_list;; - : int * int = (18, 19) #
Comments
Post a Comment