Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 1+2*3;;
- - : int = 7
- # let pi = 4.0 *. atan 1.0;;
- val pi : float = 3.14159265358979312
- # let square x = x *. x;;
- val square : float -> float = <fun>
- # square(sin pi) +. square(cos pi);;
- - : float = 1.
- # 1.0 * 2;;
- Error: This expression has type float but an expression was expected of type int
- # let rec fib n =
- if n < 2 then n else fib(n-1) + fib(n-2);;
- val fib : int -> int = <fun>
- # fib 10;;
- - : int = 55
- (*FUNCIONES*)
- # let <f> = function <x> -> <e>
- # let <f> <x> = <e>
- # function x ->
- if x>=0 then x
- else -x;;
- # let abs x = if x >= 0 then x else –x;;
- (*FUNCIONES RECURSIVAS*)
- # let fact n = if n = 0 then 1
- else n * fact(n-1);;
- ----------------------------------------------------------------------
- (*Forma correcta*)
- # let rec fact n = if n=0 then 1
- else n * fact(n-1);;
- (*LISTA*)
- # [1;3;3] @ [7,9];; //concatenación
- # list.lenght [true:false];;
- # list.map //aplica una función a toda una lista
- # let par n = n mod = 0;;
- # list.filter par [3;7;9;12;3;4] //sale la lista de pares
- # list.for_all //devuelve un bool si todos son pares
- (*ÁRBOLES*)
- let rec size (GT(r,l)) =
- list.fold_left (+) 1 (list.map size l)
- let rec size = function
- GT(r,h::t) -> size h + size (GT(r,t))
- | GT (r, []) -> 1
- type 'a tree =
- Empty
- |Node of 'a * 'a tree * 'a tree;;
- let rec height = function
- Empty -> 0
- | Node (_,i,d) -> 1 + max (height i) (height d);;
- let rec is_perfect = function
- Empty -> true
- | Node (_,Empty,b) | Node (_,b,Empty) -> is_perfect b
- | Node (_,i,d) -> height i = height d &&
- is_perfect i && is_perfect d;;
- let rec bigtree n =
- if n = 0 then Empty
- else Node ((),bigtree (n-1), bigtree(n-1));;
- let rec bigtree2 n =
- if n = 0 then Empty
- else let b = bigtree2 (n-1) in
- Node ((),b,b);;
Advertisement
Add Comment
Please, Sign In to add comment