SHOW:
|
|
- or go back to the newest paste.
| 1 | # 1+2*3;; | |
| 2 | - : int = 7 | |
| 3 | ||
| 4 | # let pi = 4.0 *. atan 1.0;; | |
| 5 | val pi : float = 3.14159265358979312 | |
| 6 | ||
| 7 | # let square x = x *. x;; | |
| 8 | val square : float -> float = <fun> | |
| 9 | ||
| 10 | # square(sin pi) +. square(cos pi);; | |
| 11 | - : float = 1. | |
| 12 | ||
| 13 | # 1.0 * 2;; | |
| 14 | Error: This expression has type float but an expression was expected of type int | |
| 15 | ||
| 16 | # let rec fib n = | |
| 17 | if n < 2 then n else fib(n-1) + fib(n-2);; | |
| 18 | val fib : int -> int = <fun> | |
| 19 | ||
| 20 | # fib 10;; | |
| 21 | - : int = 55 | |
| 22 | ||
| 23 | ||
| 24 | (*FUNCIONES*) | |
| 25 | ||
| 26 | # let <f> = function <x> -> <e> | |
| 27 | # let <f> <x> = <e> | |
| 28 | # function x -> | |
| 29 | if x>=0 then x | |
| 30 | else -x;; | |
| 31 | # let abs x = if x >= 0 then x else –x;; | |
| 32 | ||
| 33 | (*FUNCIONES RECURSIVAS*) | |
| 34 | ||
| 35 | # let fact n = if n = 0 then 1 | |
| 36 | else n * fact(n-1);; | |
| 37 | ---------------------------------------------------------------------- | |
| 38 | (*Forma correcta*) | |
| 39 | # let rec fact n = if n=0 then 1 | |
| 40 | else n * fact(n-1);; | |
| 41 | ||
| 42 | (*LISTA*) | |
| 43 | ||
| 44 | # [1;3;3] @ [7,9];; //concatenación | |
| 45 | # list.lenght [true:false];; | |
| 46 | # list.map //aplica una función a toda una lista | |
| 47 | # let par n = n mod = 0;; | |
| 48 | # list.filter par [3;7;9;12;3;4] //sale la lista de pares | |
| 49 | # list.for_all //devuelve un bool si todos son pares |