r/ocaml • u/pmmeifyouwantmee • Sep 08 '19
using recursion to implement for loop, it's so new to me and frustrating.
I'm doing a homework that can be so easily done in java but so hard in ocaml.
3
u/yawaramin Sep 10 '19
It takes time and some patience for functional programming to soak into your brain. But it’s worth it. And it’s not that it’s fundamentally more difficult than imperative style (for loops etc.). It’s just a different way of thinking. People who learned FP from the beginning found imperative style difficult too.
But maybe this will help: https://www.coursera.org/learn/programming-languages the lecture videos are great (and short), they explain the functional concepts succinctly and precisely.
-1
u/3nk1namshub Sep 08 '19 edited Sep 08 '19
The first issue I see is that your comparison operator is wrong. You're using = instead of ==. It turns out what I had read on this was wrong, and I stand corrected.
The second is that you're testing if k is greater than or equal to n, and then in the else, testing if k is less than n. However, if the else is reached, you already know that's the case.
Here's what I would change it to (formatted a bit differently though):
let rec loop k =
if k >= n
then []
else if n % k = 0
then k :: (loop (succ k))
else (* Do what you need to here *)In general, a for loop can be implemented with tail recursion as follows:
let rec loop index state =
if (some condition)
then state
else loop (succ index) (change state)Where (some condition) is a condition you want to test, and (change state) is whatever you're doing to your state. Here's an example that uses tail recursion for a factorial:
let rec fact x accum =
match x with
| 0 -> accum
| _ -> fact (x-1) (accum*x)
;;
let mynum = fact 3 1Hope I could help!
2
u/pmmeifyouwantmee Sep 08 '19
thank you very much!!! succ k is implment or decrement right?
2
u/3nk1namshub Sep 08 '19
It's short for successor, so it's increment. It's the equivalent of
(k+1), I just write itsucc k6
u/eras Sep 08 '19
The first issue I see is that your comparison operator is wrong. You're using = instead of ==.
Whaat? You should be using
=instead of==. The latter is for physical object identity, which is almost always what one doesn't want, while the former is structural equality.In this case it doesn't matter, though, as for numbers
=and==- and other unboxed objects - they return the same value. But given how"hi" == "h" ^ "i"isFalse, you should perhaps not learn into the habit, even if the polymorphic structural equality operator is not what is used in a serious context.1
u/3nk1namshub Sep 08 '19
Whaat? You should be using = instead of ==. The latter is for physical object identity, which is almost always what one doesn't want, while the former is structural equality.
Really? Everything I've seen on the subject has said to use
==. I stand corrected then, sorry.
5
u/mk270 Sep 09 '19
Been using OCaml for more than ten years - I have used for-loops two or three times ever; I have to look up the syntax each time.
Effectively, List.iter, List.map and recursion cover all the usual cases.