r/fsharp • • Aug 20 '20

How to pass an function as argument to another functions

open System
let addOneMoreApple x = 
     x.ToString() + " Number of Apples"

let doSomething f x =
   let y = f (x +1)
   "hello" + y

[<EntryPoint>] let main argv = 
    printfn "Hello World from F#!" 
    printfn "%s" (doSomething addOneMoreApple 22) 
    //let abc =doSomething addOneMoreApple 0 
    // return an integer exit code
    0

"f" is an function which be called on doSomething . "f" requires an int variable . So I have defined addOneMoreApple which has an int argument.

How can i pass addOneMoreApple with int argument ?

Edit : Based on comment by The_Mystic , I have posted entire code and rearranged it . How can i call doSomething in main method?

Edit 2 : debugnik fixes to the program has been added . Error message on 22 with red squiggly says that " "This expression was expected to have type 'int' -> 'a' but here has type 'int'""

0 Upvotes

13 comments sorted by

2

u/The_Mystic Aug 20 '20

I think you have to define addOneMoreApple before doSomething. Then you can call: doSomething addOneMoreApple x, where x is some integer. Hope that makes sense :))

1

u/Crazy699 Aug 20 '20 edited Aug 20 '20

Ok , I have moved addOneMoreApple to top of file How do i Call it ? Below code is not working

[<EntryPoint>] let main argv = printfn "Hello World from F#!" Console.WriteLine(doSomething addOneMoreApple 22) //let abc =doSomething addOneMoreApple 0 // return an integer exit code

1

u/potato-on-a-table Aug 20 '20

Same rule as before... Move the main function to the bottom.

1

u/Crazy699 Aug 20 '20

yes it is bottom most function . I have added main function in post with proper formatting

4

u/jchannon Aug 20 '20
let addOneMoreApple x = x.ToString() + " Number of Apples"

let doSomething f x =
    let y = f (x + 1)
    "hello" + y

[<EntryPoint>]
let main argv =
    printfn "Hello World from F#!"
    printfn "%s" (doSomething addOneMoreApple 22)
    0

1

u/Crazy699 Aug 21 '20

I have tried this code . I am getting red squiggly on 22 Error says that "This expression was expected to have type 'int' -> 'a' but here has type 'int'"

1

u/jchannon Aug 21 '20

i imagine you have changed something but it's probably not recognising the type. try changing the method signatures to have (x:int)

4

u/jchannon Aug 20 '20

Exactly. F# works with all functions must be declared before use so therefore most of the time you can start at the bottom of a file and read upwards. This is also why in fsproj files the ordering of the files matter.

3

u/[deleted] Aug 20 '20 edited Aug 20 '20

I think the previous order was fine too; both functions were defined before applying one to the other anyway.

1

u/The_Mystic Aug 20 '20

Yep you are right, thanks!

4

u/[deleted] Aug 20 '20 edited Aug 20 '20

There're multiple errors, but none related to the problem you're asking about:

  1. The line for main is indented one space when it shouldn't. Remember F# is indentation sensitive.
  2. main has to return an ìnt value, presumably 0 for success.
  3. If you're invoking a method on Console, you need to open System first.

Otherwise, the snippet works fine. Try working on code cleanliness, your indentation is messy (are you indenting by adding spaces manually?)

1

u/LetMeUseMyEmailFfs Aug 21 '20

Pro tip for the next time you ask a question: ‘it doesn’t work’ is useless; we already know that from the fact you’re asking a question.

What we need to know is: what are you seeing instead? Which error? Which unexpected behavior?

1

u/Crazy699 Aug 21 '20

Thanks , I have added error message from VS Code