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:45]
alfred
functional:fsharp [2020/05/09 09:25] (actual)
Línea 44: Línea 44:
  
  
-==== 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 72: 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 92: 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: 
 +<​code>​
 let daughter = "​Melissa"​ let daughter = "​Melissa"​
 let pet = "​Sugar"​ let pet = "​Sugar"​
- +</​code>​ 
-Strings can be single- or multi-line. To enter multi-line strings, simply enter a newline at the end of each line of the string+Podemos hacer strings ​multi-línea
 +<​code>​
 let haiku = "No sky let haiku = "No sky
 no earth - but still no earth - but still
 snowflakes fall" snowflakes fall"
- +</​code>​ 
-If you want to enter a single, long string on multiple lines, use the backslash character to continue the string on the next line. This will prevent a newline from being inserted. +O strings de una sóla línea en múltiples acabando estas en \ : 
 +<​code>​
 let haiku = "No sky\ let haiku = "No sky\
 no earth - but still\ no earth - but still\
 snowflakes fall" snowflakes fall"
 +</​code>​ 
 +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: 
 +<​code>​
 > let countryBytes = "​USA"​B > let countryBytes = "​USA"​B
 val countryBytes : byte array = [|85uy; 83uy; 65uy|] val countryBytes : byte array = [|85uy; 83uy; 65uy|]
 +</​code>​ 
 +Existen varias operaciones que podemos hacer con un string: 
 +<​code>​
 let fullname = "Mary "+ "​Smith" ​         // concatenation let fullname = "Mary "+ "​Smith" ​         // concatenation
 let s = "hello " + string 100            // concatenation,​ string conversion operator let s = "hello " + string 100            // concatenation,​ string conversion operator
 let len = fullname.Length ​               // 10 let len = fullname.Length ​               // 10
 let i = fullname.StartsWith("​Mary"​) ​     // true let i = fullname.StartsWith("​Mary"​) ​     // true
 +</​code>​
 +También podemos construir strings largos utilizando ''​StringBuilder'':​
 +<​code>​
 +let buf = new System.Text.StringBuilder()
 +buf.Append("​Mary had ")
 +buf.Append("​a little lamb, it's ")
 +buf.Append("​(you know the rest...)"​)
 +</​code>​
  
  
-let buf new System.Text.StringBuilder()+==== Condicionales ==== 
 +Existe el tipo de dato boolean: 
 +<​code>​ 
 +let 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'sHemos 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"​
  
-buf.Append("Mary had ")+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''​.
  
-buf.Append("a little lamb, it'​s ​")+==== Bucles ==== 
 +Los bucles son expresiones que evaluan como ''​unit''​. 
 +=== for..do === 
 +<​code>​ 
 +for i = 1 to 10 do 
 +    printfn ​"i = %d" ​i
  
-buf.Append("(you know the rest...)")+// Count from 1 to 10, with identifiers 
 +let a, b = 1, 10 
 +for k = a to b do 
 +    printfn ​"k = %d" ​k
  
-To retreive the built-up string, you can use the ToString() method:+// Count down from 10 (downto 1 
 +for j = 10 downto 1 do 
 +    printfn "j = %d" j
  
-let str buf.ToString()+// Sum the first 5 non-zero integers. 
 +// This is quite "​imperative."​ We'll see its functional cousin later. 
 +let mutable sum 
 +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
  
-printfn "%s" ​str +// 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>​
  
 ===== Notas ===== ===== Notas =====
functional/fsharp.1296146741.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)