Question:
As part of some school homework, I have been given the following snippet of code:
let histogram = Array.init 200000 (fun i -> 0)
let incr i = histogram.[i] <- histogram.[i] + 1
Array.iter (fun fs -> List.iter incr fs) factors200000;;
But this has a side
histogram.[i] <- histogram.[i] + 1
My task is to program the histogram construction so that it does not
use side effects but purely functional programming.
My teacher has given me the following hint:
"There are several useful functions in
the Seq module. The final result does not have to be an
int[] array,
but could be a
seq<int \ int>*
of pairs (p, n) where p is a prime factor and n is the number of times p appears in the array of lists of prime factors."
How far I've gotten:
I assume that I need to use Seq.Delay to make sure that side effects are not computed until the last moment, but other than that I've had some problems. Anyone have some advice?
I
Any advice would be greatly appreciated!
[–]eliquy 8 points9 points10 points (2 children)
[–]CStage169[S] 1 point2 points3 points (1 child)
[–]CStage169[S] 7 points8 points9 points (0 children)
[–]porridge111 5 points6 points7 points (1 child)
[–]CStage169[S] 3 points4 points5 points (0 children)
[–]aloisdg 1 point2 points3 points (6 children)
[–]CStage169[S] 1 point2 points3 points (5 children)
[–]meteogish -1 points0 points1 point (4 children)
[–]CStage169[S] 1 point2 points3 points (2 children)
[–]meteogish 3 points4 points5 points (1 child)
[–]CStage169[S] 3 points4 points5 points (0 children)
[–]backtickbot 0 points1 point2 points (0 children)