24 ianuarie 2015

Exemple, exercitii in SML

tuple:    (1, 2, 3);
record: {unu=1, doi=2, trei=3};
list:       [1, 2, 3];

list concat:     [1,2] @ [2,3];
list append:    1::2::3::nil;
string concat: "ab" ^ "bc";

;; value declaration
val a = 2; val (a, b, c) = (1, 2, 3);
;; wildcard
val (a, _, c) = (1,2,3);

;; functie fara nume
(fn x => x+1);
;; functie cu nume
f: val f = (fn x => x+1); f 2;
;; aceeasi functie
fun g (x) = x + 1;

map f [1,2,3];

;; functie cu cazuri speciale
fun f 1 = 1 | f (x) = x + 1;

;; infix
fun minus (a,b) = a - b;
infix minus;
6 minus 2;

;;functii recursive
fun fact (x) = if x=0 then 1 else n*fact(n-1);
;; SAU
val rec fact = (fn x => if x=0 then 1 else fact(n-1));

;; functii mutual recursive
fun odd (x) = ..... and even (x) = ..... ;

;; andalso, orelse - operatii logice doar pe booleeni

;; functie locala
local ... in ... end;

-----------------------------------------------

1) un numar e putere a lui 2

fun pow2 1 = true
| pow2 x = (x mod 2 = 0) andalso pow2 (x div 2);

pow2 8;
pow2 10;


2) inserarea unui nr. la o lista sortata a.i. lista ramane sortata

fun insert (x, []) = x::nil
| insert (x, y::ys) =
if x < y then x::y::ys else y::insert(x,ys);

insert (3, []);
insert (1, [2,3,4]);
insert (5, [4,7,9]);


3) sortare prin insertie (folosind functia de mai sus)

fun insertionSort [] = nil
| insertionSort (m::myList) = insert (m, insertionSort myList);

insertionSort [5,2,7,4,1,0,8,9];


4) toate submultimile unei multimi

local
    fun add x ys = x :: ys
in
    fun subsets1 []      = [[]]
      | subsets1 (x::xr) = subsets1 xr @ map (add x) (subsets1 xr)
end;

subsets1 [1,2,3];


5) o structura de date pt. coordonate in 2D si aflarea distantei dintre 2 puncte

(* coords = datatype name
Cords = constructor *)
datatype coords = Cords of real * real;

val p1 = Cords (1.2, 3.4);

fun sq (x:real) = x * x;

fun distance (Cords(x1, y1):coords, Cords(x2, y2):coords) =
Math.sqrt (sq (x2-x1) + sq (y2-y1));

distance (Cords (1.0, 2.0), Cords (1.1, 2.1));

(* alta s.d. pt o persoana *)
datatype person = Pers of string * string * int;
val x = Pers ("anna", "ehcat", 25);


6) Variabile-referinta
val i = ref 10; 
inc(i);
dec(i);
i := 20;


7) alt exercitiu cu structuri de date

datatype item
  = Vgn of string * real
  | Vgt of string * real
  | Omn of string * real;

fun isVegan (Vgn _) = true
| isVegan _ = false;

fun findVegan [] = []
| findVegan (m::mylist) = if isVegan m then m::findVegan(mylist) else findVegan mylist;

(*
findVegan [Vgn ("apple", 2.7), Vgt ("milk", 1.2), Omn ("beef", 5.5), Vgn ("coconut", 3.3)];
*)

(* FIND THE CHEAPEST OMNIV. FOOD *)

fun findCheapO ([], product) = product
| findCheapO ((Omn (name, price))::rest, Omn(name2, price2)) =
if price < price2 then findCheapO (rest, Omn(name, price))
else findCheapO (rest, Omn(name2, price2))
| findCheapO (m::other, cheapest) =
findCheapO(other, cheapest);

fun findCheapestOmni mylist = findCheapO (mylist, Omn("dummy", 999.9));

val superStore = [Vgn ("apple", 2.7), Vgt ("milk", 1.2), Omn ("beef", 5.5), Vgn ("coconut", 3.3), Omn("fish", 3.2), Omn("eggs", 7.8)];

(*
findCheapestOmni superStore;
*)

(* FIND THE PRICE OF AN ORDER *)

fun getPrice (Vgn (name, price)) = price
| getPrice (Vgt (name, price)) = price
| getPrice (Omn (name, price)) = price;

fun getName (Vgn (name, price)) = name
| getName (Vgt (name, price)) = name
| getName (Omn (name, price)) = name;

datatype order = Ord of string * int;

fun lookup (myorder, []) = ~1.0
| lookup (Ord(name, quant), m::mylist) =
if name = getName (m) then getPrice(m)
else lookup(Ord(name, quant), mylist);

(*
lookup(Ord("beef", 2), superStore);
*)


(* input: an order & a menu
output: total cost of the order *)

fun cost (order, []) = 0.0
| cost (Ord(name, quant), mylist) =
let val price = lookup (Ord(name, quant), mylist)
in price * real(quant)
end;

(*
cost(Ord("beef", 2), superStore);
*)

(* input: a list of orders & a menu
output: the total cost of all the orders
*)

fun totalCost ([], menu, cost1) = cost1
| totalCost (orderList, [], cost1) = cost1
| totalCost (ord::orderList, menu, cost1) =
totalCost (orderList, menu, cost(ord, menu) + cost1);

totalCost ([Ord("beef", 2), Ord("fish", 3)], superStore, 0.0);

------------------------------

8) al n-lea element dintr-o lista

fun nth ([], n) = ~99999
| nth (mylist, 0) = ~99999
| nth (m::mylist, 1) = m
| nth (m::mylist, n) = nth (mylist, n-1);

nth ([5,8,3,1,0], 4);


9) elementul de la jumatatea listei

fun middle ([]) = ~99999
| middle (mylist) = nth (mylist, length (mylist) div 2 + 1);

middle ([1,2,3,4,5,6]);


10) produsul cartezian al doua multimi

fun cartesian (nil, blist) = nil
| cartesian (alist, nil) = nil
| cartesian (a::alist, b::blist) =
(a,b) :: cartesian (a::nil, blist) @ cartesian(alist, b::blist);

cartesian (["a","b","c"],[1,2]);

Niciun comentariu: