Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
functional:fsharp [2011/01/28 16:14] alfred |
functional:fsharp [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 140: | Línea 140: | ||
| buf.Append("(you know the rest...)") | buf.Append("(you know the rest...)") | ||
| </code> | </code> | ||
| + | |||
| + | |||
| ==== Condicionales ==== | ==== Condicionales ==== | ||
| Existe el tipo de dato boolean: | Existe el tipo de dato boolean: | ||
| Línea 171: | Línea 173: | ||
| let a, b = 100, 200 | let a, b = 100, 200 | ||
| (if a < b then "a < b" else "a is not less than b") |> ignore | (if a < b then "a < b" else "a is not less than b") |> ignore | ||
| + | </code> | ||
| + | Los if's son expresiones que evaluan como ''unit''. | ||
| + | |||
| + | ==== Bucles ==== | ||
| + | Los bucles son expresiones que evaluan como ''unit''. | ||
| + | === for..do === | ||
| + | <code> | ||
| + | for i = 1 to 10 do | ||
| + | printfn "i = %d" i | ||
| + | |||
| + | // Count from 1 to 10, with identifiers | ||
| + | let a, b = 1, 10 | ||
| + | for k = a to b do | ||
| + | printfn "k = %d" k | ||
| + | |||
| + | // Count down from 10 (down) to 1 | ||
| + | for j = 10 downto 1 do | ||
| + | printfn "j = %d" j | ||
| + | |||
| + | // Sum the first 5 non-zero integers. | ||
| + | // This is quite "imperative." We'll see its functional cousin later. | ||
| + | let mutable sum = 0 | ||
| + | for n = 1 to 5 do | ||
| + | sum <- sum + n | ||
| + | printfn "current sum = %d" sum | ||
| + | </code> | ||
| + | === while === | ||
| + | <code> | ||
| + | // Output numbers from 1-10 and squares | ||
| + | let mutable n = 1 | ||
| + | while n <= 10 do | ||
| + | let sq = n * n | ||
| + | printfn "%d %d" n sq | ||
| + | n <- n + 1 | ||
| + | |||
| + | // Note mutable applies to both identifiers below | ||
| + | let mutable a, b = 1, 10 | ||
| + | while b > a do | ||
| + | printfn "%d %d" a b | ||
| + | a <- a + 1 | ||
| + | b <- b - 1 | ||
| + | |||
| + | // Using parentheses and Boolean conjuction | ||
| + | let mutable a, b, c = 1, 10, 0 | ||
| + | while ((a < b) && (c < 3)) do | ||
| + | printfn "%d %d %d" a b c | ||
| + | c <- c + 2 | ||
| </code> | </code> | ||