How to get numbers after comma in F# - f#

I want to get 4 numbers after comma in F# :
This is my code :
// Learn more about F# at http://fsharp.org
// See the 'F# Tutorial' project for more help.
open System
[<EntryPoint>]
let main argv =
let num = Console.ReadLine() |> float
Console.WriteLine(num)
Console.Read() |>ignore
0

And this's result :
open System
[<EntryPoint>]
let main argv =
let num = Math.Round(Console.ReadLine() |> float, 3)
Console.WriteLine(num)
Console.Read() |>ignore
0

Related

The block following this 'let' is unfinished, F# error

I've been trying to create a code in F# that can read text via another file, however I keep seeing a
The block following this 'let' is unfinished. Every code block is an expression and must have a result
on the
let a = line.Split delim
,I looked it up and thought it was an indentation error but that didn't fix that. Any advice?
[<EntryPoint>]
let main argv =
let delim = ','
use stream = new StreamReader #"final.txt"
let line = stream.Readline()
let a = line.Split delim
|>Seq.map System.Int32.Parse
|>Seq.toArray
printfn "Orignal numbers: %A" a
printfn "Ordered numbers: %A" (oddEven a)
0 // return an integer exit code
You're missing indentation (and misspelled a thing or two). Anything that you want to be part of a let block has to be indented one level. Here's your code properly formatted:
[<EntryPoint>]
let main argv =
let delim = ','
use stream = new StreamReader #"final.txt"
let line = stream.ReadLine()
let a =
line.Split delim
|> Seq.map System.Int32.Parse
|> Seq.toArray
printfn "Orignal numbers: %A" a
printfn "Ordered numbers: %A" (oddEven a)
0 // return an integer exit code

F# array sum can't print

So far, I have pretty much things set but that dumb printfn is still not working.
open System
[<EntryPoint>]
let main argv =
let n = Console.ReadLine() |> int
let nums = seq { for i in 1..n -> Console.ReadLine() |> int }
printfn "%d" (Seq.sum nums)
0
The answer from Joseph explains what is wrong with your code.
If you wanted to do this in a more F# way, then you'd probably want to eliminate the mutation altogether. One reasonably nice way of doing this would be to use sequence expressions to construct a sequence of all the numbers that you're reading from the console and then use Seq.sum to calculate the sum:
[<EntryPoint>]
let main argv =
let n = Console.ReadLine() |> int
let nums = seq { for i in 1..n -> Console.ReadLine() |> int }
printfn "%d" (Seq.sum nums)
0
Two things I noticed, you need to end the program with 0, that is the exit code.
Second thing is the equals sign in FSharp isn't used for updating values, F# uses the <- operator instead. Here is your program with the updated changes.
open System
[<EntryPoint>]
let main argv =
let mutable sum = 0
let n = Console.ReadLine() |> int
for i in 1..n do
let mutable r = Console.ReadLine() |> int
sum <- sum + r;
printfn "%d" sum
0

How to execute specific functions with input pattern in F# language

I'm kinda new to F# and trying out a simple calculator app. I take input from the user, and I want to the specific functions to be executed as per the input.
Right now, whenever I take any input from user, the program executes top to bottom. I want it to execute only specific functions matching with the input. Like if input is 6 then body of scientificFun() should be executed. Right now it executes all functions. Please help, I'm kinda stuck on this one!
The code is
open System
let mutable ok = true
while ok do
Console.WriteLine("Choose a operation:\n1.Addition\n2.Substraction\n3.Multiplication\n4.Division\n5.Modulo\n6.Scientific")
let input= Console.ReadLine()
let add =
Console.WriteLine("Ok, how many numbers?")
let mutable count = int32(Console.ReadLine())
let numberArray = Array.create count 0.0
for i in 0 .. numberArray.Length - 1 do
let no = float(Console.ReadLine())
Array.set numberArray i no
Array.sum numberArray
let sub x y = x - y
let mul x y = x * y
let div x y = x / y
let MOD x y = x % y
let scientificFun() =
printfn("1. Exponential")
match input with
| "1" -> printfn("The Result is: %f") (add)
| "6" -> (scientificFun())
| _-> printfn("Choose between 1 and 6")
Console.WriteLine("Would you like to use the calculator again? y/n")
let ans = Console.ReadLine()
if ans = "n" then
ok <- false
else Console.Clear()
You should define add as function: let add() = or let add inputNumbers =
Otherwise this simplified version below only executes the functions corresponding to the input number:
open System
[<EntryPoint>]
let main argv =
// define your functions
let hellofun() =
printfn "%A" "hello"
let worldfun() =
printfn "%A" "world"
let mutable cont = true
let run() = // set up the while loop
while cont do
printfn "%A" "\nChoose an operation:\n 1 hellofunc\n 2 worldfunc\n 3 exit"
let input = Console.ReadLine() // get the input
match input with // pattern match on the input to call the correct function
| "1" -> hellofun()
| "2" -> worldfun()
| "3" -> cont <- false;()
| _ -> failwith "Unknown input"
run() // kick-off the loop
0
The [<EntryPoint>] let main argv = is only necessary if you compile it. Otherwise just execute run().

How can I interpret property based test code?

How can I interpret property based test code?
I'm struggling to translate the instructions on the following snippet:
let myProperty = Prop.forAll fiveAndThrees <| fun number ->
let actual = transform number
let expected = "jbdhjsdhjdsjhsdglsdjlljh"
expected = actual
Check.QuickThrowOnFailure myProperty
Specifically, I'm struggling with the backwards pipeline operator (i.e. "<|").
Here's the test:
[<Fact>]
let ``FizzBuzz.transform returns FizzBuzz`` () =
let fiveAndThrees = Arb.generate<int> |> Gen.map ((*) (3 * 5))
|> Arb.fromGen
let myProperty = Prop.forAll fiveAndThrees <| fun number ->
let actual = transform number
let expected = "jbdhjsdhjdsjhsdglsdjlljh"
expected = actual
Check.QuickThrowOnFailure myProperty
Can someone please guide me step by step on how this code works?
Could this be rewritten using the forward pipe operator (i.e. "|>")?
This answer only covers why <| is used instead of |>.
Here are 5 examples that work toward making use of <| with a large function. The 6th example is to show how the code looks using |> instead of <|. The point is that with the 6th example using |> you have to look into the code to find the primary function funThatNeedsListAndFunc but with <| the primary function funThatNeedsListAndFunc is obvious. So you typically see <| when the last parameter is a function and you want to pass in the function after the primary function for easier comprehension. That's all; don't read more into than that.
After reading the Mark's blog I also learned that <| is useful to remove ( ) around fun. An example using ( ) is given as example 7.
let funThatNeedsListAndFunc list func =
func list
let func = List.sum
let list = Seq.toList { 0 .. 5}
let result1 = funThatNeedsListAndFunc list func
printfn "result1: %A" result1
let result2 = funThatNeedsListAndFunc list <| func
printfn "result2: %A" result2
let result3 = funThatNeedsListAndFunc list <| List.sum
printfn "result3: %A" result3
let result4 = funThatNeedsListAndFunc list <|
fun (list : 'a list) -> List.sum list
printfn "result4: %A" result4
let result5 = funThatNeedsListAndFunc list <|
fun (list : 'a list) ->
// This will be a long function
let a = 1
let b = 2
let c = a * b
let result = List.sum list
let d = "more useless lines"
let (e,f,g) = ("a", 15, 3.0)
result
printfn "result5: %A" result5
.
let result6 =
fun (list : 'a list) ->
// This will be a long function
let a = 1
let b = 2
let c = a * b
let result = List.sum list
let d = "more useless lines"
let (e,f,g) = ("a", 15, 3.0)
result
|> funThatNeedsListAndFunc list
printfn "result6: %A" result6
.
let result7 =
funThatNeedsListAndFunc list (fun (list : 'a list) ->
// This will be a long function
let a = 1
let b = 2
let c = a * b
let result = List.sum list
let d = "more useless lines"
let (e,f,g) = ("a", 15, 3.0)
result)
printfn "result7: %A" result7

first-class function

Lets say I have a function
let makeMonitoredFun f =
let c = ref 0
(fun x -> c := !c+1; printf "Called %d times.\n" !c; f x);;
Why I am not allowed to do this.
let mrev = makeMonitoredFun List.rev
So presumably you are referring to the value type restriction you get when you try to compile the code. If you add a type annotation it will work fine/. For the details of value type errors see this article http://blogs.msdn.com/b/mulambda/archive/2010/05/01/value-restriction-in-f.aspx from one of the F# developers
I was able to run the following code as the compiler could infer the type of listRevCounter.
let makeMonitoredFun f =
let c = ref 0
(fun x -> c := !c+1; printf "Called %d times.\n" !c; f x)
let listRevCounter = makeMonitoredFun List.rev
let revList = listRevCounter [ 1; 2; 3 ]

Resources