views:

187

answers:

3

I'm having a syntax error. I want to take the floor of a function that returns a floating point number.

I thought this would give me the right answer

let cyclesPerInterrupt bps bpw cpu factor = 
 floor (fudge (float(factor) cyclesPerWord cpu wordsPerSec bps bpw))

But it doesn't. I've tried everything I can think of, and it's just not coming together for me. I know it's something stupid, but I can't think of it.

For reference, fudge takes a float and an integer, cyclesPerWord takes 2 integers and wordsPerSec takes 2 integers. Floor takes a generic and returns a float.

+3  A: 

Note also that you can use parens to nest the function calls the way you were originally trying to, e.g.

...(cyclesPerWord cpu (wordsPerSec bps bpw))

(Without the inner set of parens above, it's kinda like you're trying to pass 4 arguments to cyclesPerWord, which is not what you want.)

Brian
+3  A: 

Alternatively, to avoid let blindness and parenthesis paralysis, use some pipelining |> :

let fudge (a : float) (b : int) =
    a

let cyclesPerWord (a : int) (b : int) =
    a

let wordsPerSec (a : int) (b : int) =
    a

let cyclesPerInterrupt bps bpw cpu factor =
    wordsPerSec bps bpw
    |> cyclesPerWord cpu
    |> fudge factor
    |> floor
simonuk
A: 

Looking at your function definition, it seems like you are using a C# like syntax for calling your functions, the function name exists right before the ( ) and the associated parameters for that function are within the ( ). An example would be FunctionName(Parameter1 Parameter2). F# doesn't use that style. Instead it uses a style where the function name and associated parameters exist inside the ( ). An example of this would be (FunctionName Parameter1 Parameter2).

The correct way to express your code would be

  let cyclesPerInterrupt bps bpw cpu factor = 
    (floor (fudge (float factor) (cyclesPerWord cpu (wordsPerSec bps bpw) ) ) )

though the outermost ( ) are not really necessary.

Rich McCollister
Aaah. I've been trying to figure that out.
Paul Nathan