immutability - SML-Functional Programming -
i want write code in sml mimics of c++ like
i=i+1;
or
i++
in short want count how many times loop(recursion) running when if statement true. elucidating further. problematic code looks this:
val = 0; fun <function_name>() = if <condition> (i+1; <recursive_expression>) else expression
but problem here answer 1 no matter how many times recursion performed due immutability in sml/nj.
any ideas helpful!
since didn't post whole code, created simple 1 here:
fun evens(int_list: int list) = let fun evens_helper(int_list: int list, times: int) = if null int_list times else if null (tl int_list) if (hd int_list) mod 2 = 0 1 else 0 else if (hd int_list) mod 2 = 0 1 + evens_helper(tl int_list, times + 1) else evens_helper(tl int_list, times) in evens_helper(int_list, 0) end
to need, simplest thing create helper function inside function, exact same thing original function, take 1 argument more (your counter). if counter should activated, call recursive call counter +1. if dont want activated, call recursive function using same counter value.
if don't yet, post whole code, can see it.
Comments
Post a Comment