3. a. Buatlah datatype dengan nama TreeJalur yang cocok untuk
struktur data dengan contoh visualisasi seperti gambar di atas.
type TreeJalur=
| Lf of bool
| Br of int * TreeJalur * TreeJalur;;
4. b. Tuliskan representasi internal (concrete syntax) dari representasi
visual contoh diatas sesuai dengan datatype yang telah dibuat tadi.
Br(1, Br(2,
Br(4, Lf false, Lf false),
Lf false),
Br(3,
Br(5, Lf false, Lf false),
Br(6, Lf true, Lf true)));;
5. c. Buatlah fungsi listLeaves yang menerima sebuah TreeJalur dan menghasilkan
list yang merupakan kumpulan isi dari setiap leaf-nya dari kiri ke kanan. Untuk
contoh TreeJalur seperti diatas, list yang dihasilkan adalah: [false; false; false;
false; false; true; true]
let rec listLeaves = function
| Lf x -> [x]
| Br(v, t1, t2) -> listLeaves(t1) @ listLeaves(t2);;
6. Dapatkah anda menebak polanya? Kapan sebuah elemen pada leaf adalah true
dan kapan adalah false? Bila sudah dikenali pola-nya, buatlah fungsi isValid,
yang menerima sebuah TreeJalur dan menghasilkan nilai Boolean true jika
TreeJalur tersebut valid mengikuti pola dan false bila tidak.
let rec isValid = function
| (Lf x, acc) -> if ((acc%2=0 && x=true) || (acc%2=1 && x=false)) then true
else false
| (Br(v, t1, t2), acc) -> isValid(t1, acc+v) && isValid(t2, acc+v);;
7. Untuk tipe data Tree berikut:
type tree<‘a>=
| Lf
| Br of ‘a * tree<‘a> * tree<‘a>
Buatlah sebuah fungsi bernama “oddTree” yang menerima sebuah Tree
dan mengembalikan sebuah list yang berisi elemen-elemen ganjil pada
Tree (urutan bebas);
contoh:
oddTree (Br(1, Br(2, Br(4,Lf,Lf), Br(5,Lf,Lf)), Br(3,Lf,Lf))) hasilnya [1;3;5]
let rec oddTree = function
| Lf -> []
| Br(v, t1, t2) -> if v%2=1 then [v] @ oddTree(t1) @ oddTree(t2)
else oddTree(t1) @ oddTree(t2);;
8. Untuk tipe data Tree berikut:
type tree=
| Lf of int
| Br of int * tree * tree
Buatlah sebuah fungsi bernama “oddTree” yang menerima sebuah
Tree dan mengembalikan sebuah list yang berisi elemen-elemen
ganjil pada Tree (urutan bebas);
contoh:
oddTree (Br(1, Br(2, Lf 4, Lf 5), Lf 3)) hasilnya [1;3;5]
let rec oddTree = function
| Lf v -> if v%2=1 then [v] else []
| Br(v, t1, t2) -> if v%2=1 then [v] @ oddTree(t1) @ oddTree(t2)
else oddTree(t1) @ oddTree(t2);;
9. Buatlah fungsi bernama “isFactor” yang menerima dua buah
bilangan bulat dan memberikan nilai true bila bilangan
pertama merupakan faktor dari bilangan kedua.
contoh:
isFactor 3 9 hasilnya true
let isFactor a b = if (b%a=0) then true
else false;;
10. Buatlah fungsi (rekursif) bernama “casting” yang menerima
sebuah bilangan bulat (integer) dan menghasilkan bilangan
bulat tersebut dalam tipe float.
contoh:
casting 4 hasilnya 4.0
let rec casting a = function
| 0 -> 0.0
| x -> 1.0 + casting(x-1);;
11. Buatlah fungsi bernama “throwFirstElement” yang
mengembalikan list tanpa elemen pertamanya. Asumsi list
yang dimasukkan bukan list kosong.
contoh:
throwFirstElement [1;2;3] hasilnya [2;3]
let throwFirstElement (x::xs)= xs;;
12. Buatlah fungsi bernama “throwLastElement” yang
mengembalikan list tanpa elemen terakhirnya. Asumsi: list
masukan bukan merupakan [].
contoh:
throwLastElement [1;2;3] hasilnya [1;2]
let rec throwLastElement = function
| (x::[]) -> []
| (x::xs) -> x::throwLastElement(xs);;
13. Buatlah fungsi bernama “compareList” yang menerima dua buah
list bilangan bulat dan mengembalikan list baru yang berisi
nilai boolean perbandingan nilai elemen-elemen pada kedua
buah list pada posisi yang sama. Bila ukuran list tidak sama,
hanya bandingkan sejumlah ukuran yang sama.
contoh:
compareList ([1;2;3],[1;3]) hasilnya [true;false]
compareList ([1],[]) hasilnya []
let rec compareList = function
| ([] , []) -> []
| (_ , []) -> []
| ([] , _) -> []
| (x::xs , y::ys) -> if(x=y) then [true] @ compareList(xs, ys)
else [false] @ compareList(xs, ys);;
14. Buatlah fungsi bernama “hasOddLength” yang menerima
sebuah list dan mengembalikan nilai true bila panjang list
adalah ganjil dan mengembalikan nilai false bila panjang list
adalah genap. Hint: Anda dapat menggunakan akumulator.
contoh:
hasOddElement [] hasilnya false
hasOddElement [1;2;3] hasilnya true
let rec hasOddLength = function
| ([], acc) -> if (acc%2=1) then true
else false
| (x::xs, acc) -> hasOddLength(xs, acc+1);;
15. Buatlah fungsi bernama “hasOddLength” pada soal
slide sebelumnya tanpa menggunakan akumulator.
Hint: dapat menggunakan operator not.
let rec hasOddLength = function
| [] -> false
| x::xs -> not(hasOddLength(xs));;
16. Buatlah fungsi bernama “oddList” yang menerima list bilangan
bulat dan memberikan output list yang hanya berisi bilangan
ganjil saja. Duplikasi diperkenankan.
contoh:
oddList [1;2;3;4;5] hasilnya [1;3;5]
let rec oddList= function
| [] -> []
| x::xs -> if(x%2=1) then x::oddList(xs)
else oddList(xs);;
17. Buatlah fungsi dengan nama “terurutTurun” yang menerima
sebuah list dari bilangan bulat dan memberikan output
boolean yang menyatakan apakah list bilangan bulat yang
diberikan sudah terurut mengecil atau tidak.
contoh:
terurutTurun [3;2;1] hasilnya true
let rec terurutTurun= function
| [] -> true
| x::[] -> true
| x1::x2::xs -> if(x1>x2) then terurutTurun(x2::xs)
else false;;
18. Buatlah fungsi dengan nama “persisSama” yang menerima input
dua buah list dan melakukan pengecekan apakah kedua buah
list tersebut sama persis.
contoh:
sama ([1;2;3],[1;2]) hasilnya false
let rec persisSama= function
| ([],[]) -> true
| (_,[]) -> false
| ([],_) -> false
| (x::xs, y::ys) -> if(x=y) then persisSama(xs::ys)
else false;;
19. Matriks dapat dinyatakan sebagai list dalam list namun dengan ukuran
list yang ada didalam sama semua. Bila salah satu ukuran list tidak
sama maka dianggap bukan matriks. Buatlah fungsi dengan nama
“ujiMatriks” yang memeriksa hal tsb.
contoh :
ujiMatriks [[1;2;3];[1;2;3]] hasilnya “true”.
ujiMatriks [[1;2];[1;2;3] hasilnya “false”
let rec samaUkuran=function
| ([] , []) -> true
| ([] , _) -> false
| (_ , []) -> false
| (x::xs, y::ys) -> samaUkuran(xs,ys);;
let rec ujiMatriks = function
| [] -> false
| x::[] -> true
| x1::x2::xs -> if samaUkuran(x1,x2)=true then ujiMatriks(x2::xs)
else false;;