Just Code‎ > ‎

F# - Passing functions as a parameter to another function

posted Dec 30, 2011, 1:46 AM by Peter Henell   [ updated Jan 25, 2012, 5:14 AM ]
GetNum is a function that takes an interger as parameter.
DoTen and DoZero are functions that takes: "a function that takes an integer parameter ( int -> a' ) and returns a unit" as its parameter.
let GetNum x =
    try
        let res = 100 / x
        res
    with
    | :? System.DivideByZeroException -> -1
    | :? System.OverflowException -> -2
    
let DoTen f = 
    f 10

let DoZero f = 
    f 0
    
printfn "%A" (DoTen GetNum)
printfn "%A" (DoZero GetNum)
Comments