Skip to content

Commit

Permalink
Update LA01, add quiz07 and practice functions
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-byrne committed Oct 13, 2024
1 parent a3e7baf commit 7c5ed14
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
Binary file added SML Practice.pdf
Binary file not shown.
19 changes: 19 additions & 0 deletions quiz07.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@


(* cubeLost *)

fun cubeList li = map (fn(cur) => cur * cur * cur) li;

cubeList([1,2,3]);

(* removeAll *)

fun removeAll li x = foldr (fn(cur, acc) => if cur = x then acc else cur::acc) [] li;

val x = removeAll [1,2,3,3,4] 4;

(* map question *)

fun r li = map real li

val res = r [1,2,3]
67 changes: 67 additions & 0 deletions sml_practice.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

(* map = fn : ('a -> 'b) -> 'a list -> 'b list *)
(* foldl = fn : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b *)
(* foldr = fn : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b *)

(* il2rl *)
(* turns an int list into a real list *)
fun il2rl a = map (fn(i) => real(i)) a

val test_il2rl = il2rl([1,2,3,4])

(* squareList *)
(* takes an int list and returns a list of all the squares of
the integer *)
fun squareList a = map (fn(i) => i * i) a

val test_squareList = squareList([1, 2, 3, 4]) (* [1,4,9,16] *)

(* sqSum *)
(* takes an int list and returns the sum of the squares of the integers in the list *)
fun sqSum d = foldr (fn (a, b) => (a * a) + b) 0 d;

val test_sqSum = sqSum([1,2,3,4]) (* 30 *)

(* dupList *)
(* takes list of any type and returns a new list where all the elements are duplicated *)
fun dupList li = foldr (fn (cur, accumulator) => [cur, cur] @ accumulator) [] li;

val test_dupList = dupList([1,2,3,4]);

(* myLength *)
(* takes a list and counts the number of elements in the list
(without using the built-in length function) *)
fun myLength li = foldr (fn (cur, accumulator) => accumulator + 1) 0 li;

val test_myLength = myLength([1,2,3,4]);

(* il2absrl *)
(* takes an integer list and returns a list containing the absolute value of each element as a real *)
fun il2absrl li = foldl (fn (cur, accumulator) => accumulator @ [real(if cur > 0 then cur else cur * ~1)]) [] li;

val test_il2absrl = il2absrl([~1, 2, 3, ~4]) (* [1.0,2.0,3.0,4.0] *)

(* countTrue *)
(* takes a function and a list and counts the number of elements in the list for which the function returns true (e.g. countTrue (fn x => x mod 2 = 0) *)
fun countTrue func li = foldr (fn(cur, accumulator) => accumulator + (if func(cur) = true then 1 else 0)) 0 li;

val test_countTrue = countTrue (fn(x) => x mod 2 = 0) [1,2,3,4,5,6]; (* 3 *)


(* max *)
(* return the max element in a list of integers *)
fun max li = foldr (fn(cur, accumulator) => if cur > accumulator then cur else accumulator) ~99999999 li;

val test_max = max [1,2,3,4];

(* member *)
(* takes an element and a list and returns true if the element is in the list and false otherwise *)
fun member isInCandidate li = foldr (fn(cur, accumulator) => if isInCandidate = cur then true else if accumulator = true then true else false) false li;

val test_member = member 4 [1,2,3,4]; (* true *)

(* pivot *)
(* takes an integer and a list of integers and returns two lists of integers where the first list contains all the elements in the original list that are less than or equal to the integer argument and the second list contains all the elements in the original list that are greater than the integer argument *)
fun pivot cutOffNum li = (foldr (fn(cur1, accumulator1) => if cur1 <= cutOffNum then accumulator1 @ [cur1] else accumulator1) [] li, foldr (fn(cur1, accumulator1) => if cur1 > cutOffNum then accumulator1 @ [cur1] else accumulator1) [] li)

val test_pivot = pivot 3 [1,2,3,4,5,1,2,3,4,5]; (* ([1,2,3,1,2,3],[4,5,4,5]) *)
45 changes: 45 additions & 0 deletions src/large_assignment_01/large_assignment_01.sml
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,48 @@ fun triangleR(a, b, c) =
end;



fun get(cur::li, 0) = cur
| get(cur::li, index) = get(li, index - 1);

val testGet = get([1,2,3,4,5], 4)


fun subList(li, startIndex, endIndex) = if startIndex = endIndex then [get(li, startIndex)] else [get(li, startIndex)] @ subList(li, startIndex + 1, endIndex);

val testSubList = subList([1,2,3,4,5,6,7,8,9,10], 4, 8);


fun reverse li = foldl (fn(cur, acc) => cur::acc) [] li;

val testReverse = reverse [1,2,3,4,5,6];



fun apply([], f) = []
| apply(x::li, f) = [f(x)] @ apply(li, f);

val testApply = apply([(1,2),(3,4),(5,6)],(op +));



fun collapse([], startVal, func) = startVal
| collapse(cur::li, startVal, func) = func(cur, collapse(li, startVal, func));

val testCollapse = collapse([1,2,3,4,5],0,(op +));



(* fun quicksort = fun *)


fun substring(str, "") = false
| substring(str, isIn) = if String.size(str) = String.size(isIn) then false else if str = String.substring(isIn, 0, String.size(str)) then true else substring(str,
String.substring(
isIn,
1,
String.size(isIn)
)
);

val testSubstring = substring("hello", "world hello")

0 comments on commit 7c5ed14

Please sign in to comment.