A reasonably idiomatic way to do this is

let a, b, c = 10, 20, 30
let total = seq { 0 .. 200000000 } |> Seq.fold (fun acc x -> acc + a * x * x + b * x + c) 0

the sequence generation and the fold could both be parallelized (?)

By on 12/29/2008 7:45 PM ()

Thanks Graham this was really usefull.

I have been trying to build this into a c# application. I have 2 problems. The first the Demo() returns an integer instead of a double. The second problem I am not sure how to make this run asynchronously

Thanks again for your help...

Nigel...

file.fs
====
#light
module UserScript
open System
let Demo() =
let a = 10
let b = 20
let c = 30
let total = seq { 0 .. 200000000 } |> Seq.fold (fun acc x -> acc + a * x * x + b * x + c) 0;
total

Then in C# I call the above as follows:

public void test()
{
DateTime Start = DateTime.Now;
double Total = UserScript.Demo();
DateTime Finish = DateTime.Now;
TimeSpan delta = Finish.Subtract(Start);
Console.WriteLine("Total = " + Total + " In " + delta.TotalMilliseconds + " ms");

}

By on 12/30/2008 2:06 AM ()

Unlike C#, F# is very particular about numeric types and will not convert back and forth between ints and doubles - so 1 + 1.0 will not compile. (Also, an F# float is a System.double... thanks for that )

If you want Demo to return a float either convert the result to a float, by returning float(total), or construct a float in the first place. For that you need to replace all the integers with floats

let Demo() =
let a = 10.0
let b = 20.0
let c = 30.0
let total = seq { 0.0 .. 200000000.0 } |> Seq.fold (fun acc x -> acc + a * x * x + b * x + c) 0.0;
total

The async part is more interesting, and more complex, and I'm punting on that one.

Async workflows are probably the most F# way to do it, though as far as I can see there's no async fold, which I think makes sense. "Expert F#" has a (brain hurting) chapter on these.

Also, Jon Harrop's "F# for Scientists" has some really quite spectacularly simple examples based on combinators (i.e. exactly the kind of thing you're not going to think of if you're coming from an imperative background) - including one that creates a thread pool based on the number of cores you have on your machine - how handy is that?

Or maybe parallel extensions are the way to go...

The hardest thing I've found with F# is learning the idioms. With C++ there was a way to do that: read Coplien's "Advanced C++" and that was (more than) what you needed to know. There are good chapters in both the F# books I mentioned, on common techniques and idioms, but there's a ton to learn. And F# is a big language, with lots of ways to do the same thing. People like that, I know, but there's a lot to be said for a language where there is a demonstrably right way to do standard tasks. It makes it a lot easier to read other people's code.

"Idiomatic F#" anyone?

By on 12/30/2008 2:16 PM ()

Not quite sure what you mean by "building the variable x"

Your code transliterated into F# looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
let a = 10


let b = 20


let c = 30


let Iterations = 2


let total = ref 0

for x = 0 to Iterations do


total := !total + a*x*x + b*x + c


done

If this code actually appeared in an F# program it would be considered disgusting because the standard functional programming style would eliminate the need for the for loop (and for the variable x).

By on 12/29/2008 7:49 AM ()

If this code actually appeared in an F# program it would be considered disgusting because the standard functional programming style would eliminate the need for the for loop (and for the variable x).

disgusting is way too strong a word. It would probably look bad in the eyes of functional programming purists, but F# is neither pure nor purely functional.

And that's enough word play with "pure' for this year ;)

By on 12/29/2008 8:10 AM ()

I am cool with this, I am just making my first steps in the funvtional world

:)

By on 12/29/2008 11:22 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper