Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Functional > in F#, how do I...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 3 Topic 2692 of 2912
Post > Topic >>

in F#, how do I...

by "mensanator@[EMAIL PROTECTED] " <mensanator@[EMAIL PROTECTED] > Dec 13, 2007 at 01:44 PM

....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?
 




 3 Posts in Topic:
in F#, how do I...
"mensanator@[EMAIL P  2007-12-13 13:44:22 
Re: in F#, how do I...
Jon Harrop <usenet@[EM  2007-12-14 13:09:31 
Re: in F#, how do I...
"mensanator@[EMAIL P  2007-12-14 17:24:27 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 4:25:48 CDT 2008.