x::xs - MADS Group

Anuncio
#let rec cuadrado = function [] -> []
| x::xs -> (x*x) :: cuadrado xs;;
#let rec doble = function [] -> []
| x::xs -> (2*x) :: doble xs;;
#let rec inc = function [] -> []
| x::xs -> (x+1) :: inc xs;;
#let rec acadena = function [] -> []
| x::xs -> (string_of_int x) :: acadena xs;;
#let rec atrue = function [] -> []
| x::xs -> true :: atrue xs;;
#let rec map f = function [] -> []
| x::xs -> f x :: map f xs;;
val map : (’a -> ’b) -> ’a list -> ’b list = <fun>
#let sqr x = x*x;;
#let cuadrado xs = map sqr xs;;
#let cuadrado = map sqr;;
#let cuadrado =
let f x = x*x
in map f;;
#let cuadrado = map (fun x -> x*x);;
#let doble
= map (fun x -> 2*x);;
#let inc
= map (fun x -> x+1);;
#let acadena
= map string_of_int;;
#let atrue
= map (fun _ -> true);;
1
#let rec suma = function [] -> 0
| x::xs -> x + suma xs;;
val suma : int list -> int = <fun>
#let rec cuenta = function [] -> 0
| x::xs -> 1 + cuenta xs;;
#let rec abst f = function [] -> 0
| x::xs -> f x + abst f xs;;
#let suma = abst (fun x -> x);;
#let cuenta = abst (fun _ -> 1);;
#let rec mult = function [] -> 1
| x::xs -> x * mult xs;;
#let rec fold f e = function [] -> e
| x::xs -> f x (fold f e xs);;
val fold : (’a -> ’b -> ’b) -> ’b -> ’a list -> ’b = <fun>
#let
#let
#let
#let
suma =
suma =
mult =
cuenta
fold (fun x y -> x + y) 0;;
fold ( + ) 0;;
fold ( * ) 1;;
= fold (fun x y -> 1+y) 0;;
#let abst f = fold (fun x y -> f x + y) 0;;
#let map f = fold (fun x y -> f x :: y) [];;
#let rec fold’ f g e = function [] -> e
| x::xs -> g (f x) (fold’ f g e xs);;
val fold’ : (’a -> ’b) -> (’b -> ’c -> ’c) -> ’c -> ’a list -> ’c = <fun>
#let suma = fold’ (fun x -> x) (+) 0;;
#let abst f = fold’ f (+) 0;;
#let fold’ f g = fold (fun x y -> g (f x) y);;
#let fold f = fold’ (fun x -> x) (fun x y -> f x y);;
2
#let rec fib = function 0 -> 1
| 1 -> 1
| n -> fib (n-1) + fib (n-2);;
#type ’a arbol = Rama of ’a arbol * ’a * ’a arbol | Vacio;;
#let rec nodos = function Vacio
-> 0
| Rama (i,x,d) -> nodos i + 1 + nodos d;;
#let rec dc es_trivial sol_trivial parte une = function
datos when es_trivial datos -> sol_trivial datos
| datos
->
let (p1,p2) = parte datos
in une (dc es_trivial sol_trivial parte une p1,
dc es_trivial sol_trivial parte une p2);;
val dc :
(’a -> bool) ->
(’a -> ’b) -> (’a -> ’a * ’a) -> (’b * ’b -> ’b) -> ’a -> ’b = <fun>
#let fib = dc (fun
(fun
(fun
(fun
n -> n=0 or n=1)
_ -> 1)
n -> (n-1, n-2))
(x,y) -> x+y);;
#let nodos = dc (function Vacio -> true | _ -> false)
(fun _ -> 0)
(fun (Rama(i,_,d)) -> (i,d))
(fun (x,y) -> x + 1 + y);;
#let fusion =
let rec parte = function x::y::xys -> let (p1,p2) = parte xys
in (x::p1,y::p2)
| xs
-> (xs,[])
and
une = function (x::xs,y::ys) when x < y -> x :: une (xs,y::ys)
|
(x::xs,y::ys)
-> y :: une (x::xs,ys)
|
(
[],
ys)
-> ys
|
(
xs,
[])
-> xs
in dc (function _::_::_ -> false | _ -> true)
(function x -> x)
parte
une;;
3
Descargar