Here is some food for thought.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 

type BitHelper =
    static member BestBit(x : int) =
        x >>> 31
    static member BestBit(x : int64) =
        x >>> 63

printfn "%A" (BitHelper.BestBit(System.Int32.MaxValue - 20))
printfn "%A" (BitHelper.BestBit(System.Int32.MinValue + 20))

printfn "%A" (BitHelper.BestBit(System.Int64.MaxValue - 20L))
printfn "%A" (BitHelper.BestBit(System.Int64.MinValue + 20L))

printfn "%A" (sign -1, sign 0, sign 1)
printfn "%A" (sign -1L, sign 0L, sign 1L)

By on 5/2/2010 2:21 PM ()

thanks for the replies guys, Im new to f# so excuse my ignorance :)

Actually what i posted was only a part of the function:

basically Im encoding negative numbers as positive, so called zigzag encoding:

for sints

let x = (x <<< 1) ^^^ (x >>> 31)

and for sint64

let (x <<< 1) ^^^ (x >>> 63)

I would like to keep it as a single function so i can do:

zigzag 12345678890L

zigzag 1234567

D.

By on 5/2/2010 2:55 PM ()

In F#, in order to have a function work on multiple data types, you must either

- use overloading (as in my previous example)

- take an argument of type 'obj' and then do type tests (and possibly fail at runtime)

- use static member constraints (advanced technique that I'm not sure if it's applicable)

I recommend either overloading or separate function names for separate data types. (Static typing is your friend, work with it.)

By on 5/2/2010 3:08 PM ()

Is there a reason that you need a single function to do both? If so, is your intent that it would be a pseudo-generic function whose behavior would be determined based on the compile-time type of the argument, or would it be a function taking any object and branching based on the runtime type of the argument?

By on 5/2/2010 2:19 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