View difference between Paste ID: pjzau89j and jvcVfLGq
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
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);;
82+
				Node ((),b,b);;
83
84
85
(*ENTRADA SALIDA*)
86
let mi_print_string s =
87
	let l = String.length s in
88
	let rec aux i =
89
		if i < l then
90
			(print_char s.[i])
91
		else ()
92
	in aux 0;;
93
94
let print_endline s = print_string s ; print_newline ();;
95
96
let rec input_line_list e =
97
	try
98
		let s = input_line e in
99
		s:: input_line_list e
100
	with End_of_file -> [];;
101
102
(*------------------------------------------*)
103
(*PROGRAMACIÓN IMPERATIVA*)
104
let contador = ref 0;;
105
let rec turno =
106
	contador := !contador + 1;
107
	!contador;;
108
109
let reset () =
110
	contador := 0;;
111
112
let fact n = 
113
	let i = ref 2
114
	and f = ref 1 in
115
	while(!i<=n) do
116
		f:= !f * !i;
117
		i:= !i + 1
118
	done;
119
	!f;;
120
121
let i = ref 0;;
122
(!);;
123
! i;;
124
i := 3+2;;
125
i := !i +1;;
126
127
128
(***************************************************)
129
type persona = (nombre : string; edad : int);;
130
131
let p = (edad = 43; nombre = "Pepe");;
132
let edad (nombre = _ ; edad = x) = x;;
133
134
(***************************************************)
135
136
class point (x_init, y_init) =
137
object 
138
	val mutable x = x_init
139
	val mutable y = y_init
140
141
	method get_X = x
142
	method get_y = y
143
144
	method moveto (a,b) = x <- a; y <- b;
145
	method rmoveto (dx,dy) = x <- x+dx; y <- y+dy
146
	method to_string () = "("^(string_of_int x)^"."^(string_of_int y)^")"
147
	method distance () = sqrt(float(x*x+y*y))
148
end;;
149
150
(***************************************************)