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 |
| 49 | + | # list.for_all //devuelve un bool si todos son pares |
| 50 | ||
| 51 | ||
| 52 | ||
| 53 | (*ÁRBOLES*) | |
| 54 | let rec size (GT(r,l)) = | |
| 55 | list.fold_left (+) 1 (list.map size l) | |
| 56 | ||
| 57 | let rec size = function | |
| 58 | GT(r,h::t) -> size h + size (GT(r,t)) | |
| 59 | | GT (r, []) -> 1 | |
| 60 | ||
| 61 | type 'a tree = | |
| 62 | Empty | |
| 63 | |Node of 'a * 'a tree * 'a tree;; | |
| 64 | ||
| 65 | let rec height = function | |
| 66 | Empty -> 0 | |
| 67 | | Node (_,i,d) -> 1 + max (height i) (height d);; | |
| 68 | ||
| 69 | let rec is_perfect = function | |
| 70 | Empty -> true | |
| 71 | | Node (_,Empty,b) | Node (_,b,Empty) -> is_perfect b | |
| 72 | | Node (_,i,d) -> height i = height d && | |
| 73 | is_perfect i && is_perfect d;; | |
| 74 | ||
| 75 | let rec bigtree n = | |
| 76 | if n = 0 then Empty | |
| 77 | else Node ((),bigtree (n-1), bigtree(n-1));; | |
| 78 | ||
| 79 | let rec bigtree2 n = | |
| 80 | if n = 0 then Empty | |
| 81 | else let b = bigtree2 (n-1) in | |
| 82 | Node ((),b,b);; |