Herramientas de usuario

Herramientas del sitio


functional:fsharp

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anterior Revisión previa
Próxima revisión
Revisión previa
functional:fsharp [2011/01/27 16:48]
alfred
functional:fsharp [2020/05/09 09:25] (actual)
Línea 45: Línea 45:
  
  
-==== Tipos de datos y operaciones ====+ 
 + 
 + 
 +===== Tipos de datos y operaciones ​=====
 Los datos en F# no se asignan sino que se enlazan. Esto significa que al hacer: Los datos en F# no se asignan sino que se enlazan. Esto significa que al hacer:
 <​code>​ <​code>​
Línea 73: Línea 76:
  
  
-=== Numéricos ===+==== Numéricos ​====
 Un número, como por ejemplo 100, es interpretado como un integer de 32 bits, si queremos que sea un short escribiríamos 100s. Los tipos de datos que existen son: Un número, como por ejemplo 100, es interpretado como un integer de 32 bits, si queremos que sea un short escribiríamos 100s. Los tipos de datos que existen son:
 {{ functional:​fsharp:​numeric_data_types.png |}} {{ functional:​fsharp:​numeric_data_types.png |}}
Línea 93: Línea 96:
 </​code>​ </​code>​
 //F# PowerPack (FSharp.PowerPack.dll)//​ es una librería de Microsoft con medidas predefinidas. //F# PowerPack (FSharp.PowerPack.dll)//​ es una librería de Microsoft con medidas predefinidas.
-=== Strings ===+ 
 +==== Strings ​====
 Para definir strings: Para definir strings:
 <​code>​ <​code>​
Línea 112: Línea 116:
 </​code>​ </​code>​
 Podemos acceder a los carácteres:​ Podemos acceder a los carácteres:​
 +<​code>​
 let daughter = "​kimberly" ​ // string let daughter = "​kimberly" ​ // string
 let first = daughter.[0] ​  // first character, '​k'​ let first = daughter.[0] ​  // first character, '​k'​
 let last = daughter.[7] ​   // last character, '​y'​ let last = daughter.[7] ​   // last character, '​y'​
 +</​code>​
 O convertir el string a bytes: O convertir el string a bytes:
 <​code>​ <​code>​
Línea 133: Línea 139:
 buf.Append("​a little lamb, it's ") buf.Append("​a little lamb, it's ")
 buf.Append("​(you know the rest...)"​) buf.Append("​(you know the rest...)"​)
 +</​code>​
 +
 +
 +==== Condicionales ====
 +Existe el tipo de dato boolean:
 +<​code>​
 +let t = true        // true
 +let f = false       // false
 +let x = t && f      // false
 +let y = t || f      // true
 +let fls = not true  // false
 +</​code>​
 +Para evaluarlo, como en otros lenguajes, utilizamos los if's. Hemos de tener en cuenta que en F# no existe el ''<​nowiki>​==</​nowiki>''​ sino el ''​=''​ para hacer comparaciones de igual. \\ 
 +En F# el if devuelve una expresión que es asignable a una variable:
 +<​code>​
 +let name, pin = "​bob",​ 123
 +let securityMessage =
 +    if name = "​bob"​ && pin = 123 then "​welcome bob!"
 +    elif name = "​sally"​ && pin = 456 then "​welcome sally!"​
 +    else "​access denied"​
 +
 +let name = "​bob"​
 +let s =
 +    if name="​bob"​ then
 +        printfn "bob is a palindrome"​
 +        "bob backwards is still bob"
 +    else
 +        printfn "​access denied"​
 +        "who are you?"
 +</​code>​
 +Para no asignar un if llamaremos a ''​ignore''​ que recibe una expresión y la obvia:
 +<​code>​
 +let a, b = 100, 200
 +(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>​
  
functional/fsharp.1296146896.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)