Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*Karol Dziachan*)
- (*task 1 - create the function, which the list of lists is flattened by one level*)
- let rec flatten list=
- List.hd list @ List.hd (List.tl list);;
- flatten[[1;2;3];[4;5;6]] = [1;2;3;4;5;6];;
- flatten[[];[1;2;3]] = [1;2;3];;
- flatten[[4;5;6];[]] = [4;5;6];;
- flatten[[];[]] = [];;
- (* task 2*)
- let rec count (x,xs) =
- if xs == [] then failwith ("empty list")
- else if List.tl xs == [] then 1
- else if List.hd xs == x then 1+count(x, List.tl xs)
- else count(x , List.tl xs);;
- count('a', ['a';'l';'a']) = 2;;
- count('a', ['a';'l';'a';'a';'m';'a'])= 4;;
- count(1, [1;2;3;1;3;1;5;3;1]) = 4;;
- count('a', []);;
- (* task3 *)
- let rec replicate (x, n) =
- if(n==1) then x
- else if(n==0) then failwith ("nothing")
- else x @ replicate(x, n-1);;
- replicate(["la"], 3);;
- replicate([""], 3);;
- replicate(["la"], 0);;
- replicate([""], 0);;
- (*task 4*)
- let rec sqrtList xs =
- if xs==[] then failwith ("nothing")
- else if List.tl xs != [] then (List.hd xs)*(List.hd xs) :: (sqrtList(List.tl xs))
- else List.tl xs @ ([(List.hd xs)*(List.hd xs)]);;
- sqrtList[1;2;3;(-4)];;
- (* task 5 *)
- let rec palindrome xs =
- if xs == [] then failwith ("empty list")
- else
- xs=(List.rev xs);;
- palindrome["a";"l";"a"];;
- palindrome["k";"a";"j";"a";"k"];;
- palindrome["k";"a";"r";"o";"l"];;
- palindrome["A";"l";"a"];;
- palindrome[];;
- (*task 6*)
- let rec listLength xs=
- if(xs == []) then failwith ("empty lity")
- else if (List.tl xs != []) then 1+listLength(List.tl xs)
- else 1;;
- listLength[1;2;3;4;5];;
- listLength["k";"a";"r";"o";"l"];;
- listLength["mother"];;
- listLength["mother"; "father"];;
- listLength[];;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement