diff --git a/SML Practice.pdf b/SML Practice.pdf new file mode 100644 index 0000000..d1c93ca Binary files /dev/null and b/SML Practice.pdf differ diff --git a/quiz07.sml b/quiz07.sml new file mode 100644 index 0000000..aba4e82 --- /dev/null +++ b/quiz07.sml @@ -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] \ No newline at end of file diff --git a/sml_practice.sml b/sml_practice.sml new file mode 100644 index 0000000..8117160 --- /dev/null +++ b/sml_practice.sml @@ -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]) *) \ No newline at end of file diff --git a/src/large_assignment_01/large_assignment_01.sml b/src/large_assignment_01/large_assignment_01.sml index 5fffcd2..92af863 100644 --- a/src/large_assignment_01/large_assignment_01.sml +++ b/src/large_assignment_01/large_assignment_01.sml @@ -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")