Closed dido18 closed 9 years ago
fun add(x int, y int) int{
return x+y;
}
fun minus(x int, y int) int{
return x-y;
}
fun main(){
var result int = 0;
var value int;
var op int = 0;
while op != 9 {
value = readln();
if op == 1 {
result = async{return add(result,value)};
}
if op==2 {
result = async{return minus(result,value)};
}
op = readln();
}
println(result);
}
This funW@p program have to be traslate in F# in the following way:
open System
open System.IO
open System.Threading.Tasks
let rec add x y =
x + y
let rec minus x y =
x - y
[<EntryPoint>]
let main argv =
let _task = ref( Map.empty ) //Map to manage the open task
let result = ref 0
let value = ref 0
let op = ref 1
while !op <> 9 do
value := Convert.ToInt32(Console.ReadLine())
if !op = 1 then
//before use the variable of a async//
if (!_task).ContainsKey(result) then
let task : Task<int> = (!_task).[result]
result := task.Result
//--------///
_task := (!_task).Add(result, Async.StartAsTask( async{ return add (!result) (!value)} ))
if !op = 2 then
//before use the variable of a async//
if (!_task).ContainsKey(result) then
let task : Task<int> = (!_task).[result]
result := task.Result
//-----//
_task := (!_task).Add(result, Async.StartAsTask( async{ return minus (!result) (!value) } ))
op := Convert.ToInt32(Console.ReadLine())
//before use the variable of a async//
if (!_task).ContainsKey(result) then
let task : Task<int> = (!_task).[result]
result := task.Result
//-----//
Console.WriteLine(!result)
Console.ReadLine() |> ignore
0
@teto1992
We have these possibility: luca has interpreted the first choice.
My choice is that an Async node is translated always as:
Async.StartAsTask( async{ return .... } )
Otherwise we must have two asyncNodes or a field in the async node that indicates that it hasn't a return valueI think that we should have a new Types 'TaskAsync' in order to discover when we must introdue a waiting condition in the f# code.