....access individual members of a tuple?
This works, but is it right?
let remainder a b =
let rem (x,y) = y
rem (BigInt.divmod a b)
....access individual members of a list?
This also works, but appears horribly inefficient...
> a;;
val it : int list = [1; 2; 3; 4]
> List.nth a 1;;
val it : int = 2
....when used to iterate through the list
let hailstone_function_parameters sv =
let mutable list_sum = 0I
let mutable list_cnt = 0I
let mutable Z = 0I
let mutable e = 0I
let svl = (List.length sv) - 1
for i = (svl) downto 0 do
// slow (due to List.nth)
list_sum <- list_sum + (List.nth sv i)
// fast
list_cnt <- list_cnt + 1I
// REAL slow
Z <- Z+((BigInt.pow 3I (BigInt.of_int i))*(BigInt.pow 2I e))
// slow
e <- e + (List.nth sv (svl - i))
done
let X = BigInt.pow 2I list_sum
let Y = BigInt.pow 3I list_cnt
(X,Y,Z)
I note List methods like .iter and .map but haven't quite
figured out how to use them or if they are faster than
List.nth. And I'm not changing every item on the list or
creating a new list, I'm creating a running sum based on
list contents.
Is a list even the right thing to use?
To creat the list, I was doing
while (ccc>1I) do
let fs = fell_swoop ccc
ccc <- t_0 fs
sv <- List.append sv [t_1 fs] // slow
done
But that's real slow also. OTOH,
while (loop_point_found=0) do
let fs = test_fell_swoop ccc
ccc <- t_0 fs
L <- L + 1
test_sv <- (t_1 fs)::test_sv // fast
if ccc=19I then
(loop_point_found <- 1)
printf "\n %d odd numbers found\n\n" L
if L>124000 then (loop_point_found <- 1)
done
works fast (we're talking a couple seconds vs almost 9 minutes)
although the list is constructed backwards. Luckily, List.rev
seems quite fast.
But is this the right way to build a list dynamically?
Should I be using something else, like an array?


|