这次回顾HW2,主要是Standard ML的模式匹配使用。

课程主页:

https://www.coursera.org/learn/programming-languages/home

B站搬运:

https://www.bilibili.com/video/BV1dL411j7L7

参考资料:

代码

(* Dan Grossman, Coursera PL, HW2 Provided Code *)

(* if you use this function to compare two strings (returns true if the same
   string), then you avoid several of the functions in problem 1 having
   polymorphic types that may be confusing *)
fun same_string(s1 : string, s2 : string) =
    s1 = s2

(* put your solutions for problem 1 here *)

(* you may assume that Num is always used with values 2, 3, ..., 10
   though it will not really come up *)
datatype suit = Clubs | Diamonds | Hearts | Spades
datatype rank = Jack | Queen | King | Ace | Num of int 
type card = suit * rank

datatype color = Red | Black
datatype move = Discard of card | Draw 

exception IllegalMove

(* put your solutions for problem 2 here *)
(* problem 1.a *)
fun all_except_option(str: string, str_list: string list) = 
   let
      (* 从str_list中删除str, 然后据此判断是否输出NONE *)
      fun filter(str_list) = 
         case str_list of
            [] => []
            | str1::str_list1 => 
               (if same_string(str, str1)
               then
                  str_list1
               else
                  str1::filter(str_list1))
     val res = filter(str_list)
   in
     if res = str_list then NONE else SOME res
   end

(* problem 1.b *)
fun get_substitutions1(strll, str) = 
   case strll of
      [] => []
      | strl::strll_rest =>
         (case all_except_option(str, strl) of
            NONE => [] @ get_substitutions1(strll_rest, str)
            | SOME list => list @ get_substitutions1(strll_rest, str)
         )

(* problem 1.c *)
fun get_substitutions2(strll, str) = 
   let
     fun helper(strll, res) = 
      case strll of
         [] => res
         | strl::strll_rest =>
            (case all_except_option(str, strl) of
               NONE => helper(strll_rest, res)
               | SOME list => helper(strll_rest, res @ list)
            )
   in
     helper(strll, [])
   end

(* problem 1.d *)
fun similar_names(strll, full_name) = 
   let
     val first_name = case full_name of {first=fir, middle=mid, last=las} => fir
     val middle_name = case full_name of {first=fir, middle=mid, last=las} => mid
     val last_name = case full_name of {first=fir, middle=mid, last=las} => las
     val names = get_substitutions2(strll, first_name)
     fun helper(names, res) =
      case names of
         [] => res
         | name::rst => helper(rst, res @ [{first=name, middle=middle_name, last=last_name}])
   in
     helper(names, [full_name])
   end

(* problem 2.a *)
fun card_color(card) = 
   case card of
      (Clubs, rank) => Black
      | (Diamonds, rank) => Red
      | (Hearts, rank) => Red
      | (Spades, rank) => Black

(* problem 2.b *)
fun card_value(card) = 
   case card of
      (_, Num i) => i
      | (_, Ace) => 11
      | (_, _) => 10

(* problem 2.c *)
fun remove_card(cs, c, e) = 
   let
      fun helper(cs, res) = 
         case cs of
            [] => res
            | c1::cs_rest =>
               if c1 = c
               then res @ cs_rest
               else helper(cs_rest, res @ [c1])
      val res = helper(cs, [])
   in
     (* 没有改变表示没有c *)
     if res = cs
     then raise e
     else res
   end

(* problem 2.d *)
fun all_same_color(cards) = 
   case cards of
      [] => true
      | card::[] => true
      | card1::(card2::cards_rst) => (card_color(card1) = card_color(card2)) andalso all_same_color(card2::cards_rst)

(* problem 2.e *)
fun sum_cards(cards) = 
   let
     fun helper(cards, res) = 
      case cards of 
         [] => res
         | card::cards_rst => helper(cards_rst, card_value(card) + res)
   in
     helper(cards, 0)
   end

(* problem 2.f *)
fun score(held_cards, goal) = 
   let
     val sum = sum_cards(held_cards)
     val pre_score =
      if sum > goal
      then 
         (sum - goal) * 3
      else 
         goal - sum
   in
     if all_same_color(held_cards)
     then
      pre_score div 2 
     else
      pre_score
   end

(* problem 2.g *)
fun officiate(card_list, move_list, goal) = 
   let
     fun helper(card_list, held_list, move_list, sum) = 
      if sum > goal
      then
         score(held_list, goal)
      else
         case move_list of
            [] => score(held_list, goal)
            (* 丢弃card *)
            | (Discard card)::move_list_rst => 
               helper(card_list, remove_card(held_list, card, IllegalMove), move_list_rst, sum -  card_value(card))
            | Draw::move_list_rst =>
               case card_list of 
                  [] => score(held_list, goal)
                  | card::card_list_rst => 
                     helper(card_list_rst, held_list @ [card], move_list_rst, sum + card_value(card))
   in
     helper(card_list, [], move_list, 0)
   end

(* problem 3.a *)
fun sum_cards_challenge(cards) = 
   let
     (* for a in arr, get a + num *)
     fun add_list(arr, num) = 
      case arr of 
         [] => []
         | a::arr_rst => (a + num)::add_list(arr_rst, num)
     (* ace = 1 or 11 *)
     fun helper(cards, res) = 
      case cards of 
         [] => res
         | (suit, Ace)::cards_rst => helper(cards_rst, add_list(res, 1) @ add_list(res, 11))
         | card::cards_rst => helper(cards_rst, add_list(res, card_value(card)))
   in
     helper(cards, [0])
   end

fun score_challenge(held_cards, goal) = 
   let
     fun helper1(sum, goal) = 
      if sum > goal
      then 
         (sum - goal) * 3
      else 
         goal - sum
      fun helper2(sum_arr, res) = 
         case sum_arr of
            [] => res
            | sum::sum_arr_rst => helper2(sum_arr_rst, Int.min(helper1(sum, goal), res))
      val flag = all_same_color(held_cards)
      val sum_arr = sum_cards_challenge(held_cards)
      val res = helper2(sum_arr, 100000)
   in
     if flag
     then 
      res div 2 
     else 
      res
   end

fun officiate_challenge(card_list, move_list, goal) = 
   let
     fun helper1(sum) = 
      if sum > goal
      then 
         (sum - goal) * 3
      else 
         goal - sum
     fun helper2(held_list, sum) = 
      let
        val res = helper1(sum)
      in
        if all_same_color(held_list)
        then
         res div 2 
        else
         res
      end
     fun helper3(card_list, held_list, move_list, sum) = 
         if sum > goal
         then
            helper2(held_list, sum)
         else
            case move_list of
               [] => helper2(held_list, sum)
               (* 丢弃card *)
               | (Discard card)::move_list_rst => 
                  helper3(card_list, remove_card(held_list, card, IllegalMove), move_list_rst, sum - card_value(card))
               (* 抽卡 *)
               | Draw::move_list_rst =>
                  case card_list of 
                     [] => helper2(held_list, sum)
                     | card::card_list_rst => 
                        case card of
                          (suit, Ace) => Int.min(helper3(card_list_rst, held_list @ [(suit, Ace)], move_list_rst, sum + 1), helper3(card_list_rst, held_list @ [(suit, Ace)], move_list_rst, sum + 11))
                        | card1 => helper3(card_list_rst, held_list @ [card1], move_list_rst, sum + card_value(card1))

   in
     helper3(card_list, [], move_list, 0)
   end

(* problem 3.b *)
fun careful_player(card_list, goal) = 
   let
     (* 找到值为v的card *)
     fun find_card(card_list, v) = 
      case card_list of
         [] => NONE
       | card::card_list_rst =>
         if card_value(card) = v
         then
            SOME card
         else
            find_card(card_list_rst, v)
     fun helper(card_list, held_list, move_list, sum) = 
      if sum - goal = 0
      then
         move_list
      else
         (* 超过10则直接丢弃, 否则判断是否应该先丢弃再拿 *)
         if goal - sum > 10
         then
            (* 判断是否可以丢弃 *)
            case card_list of 
               [] => move_list @ [Draw]
               | card::card_list_rst => 
                  helper(card_list_rst, card::held_list, move_list @ [Draw], sum + card_value(card))
         else
            case card_list of
               [] => move_list
             | card::card_list_rst => 
               let
                  (* 应该丢弃的卡片value *)
                  (* sum - v + card_value(card) = goal *)
                  val v = sum - goal + card_value(card)
                  (* 可以丢弃的卡片 *)
                  val card_discard = find_card(card_list, v)
               in
                  (* 如果可以得到0, 则直接返回, 否则丢弃卡片 *)
                  if v = 0
                  then
                     move_list @ [Draw]
                  else
                     (* 如果没有可以删除的卡片, 则判断能否丢弃, 不能丢弃则直接返回; 否则先丢弃, 然后再拿 *)
                     case card_discard of
                        NONE =>
                           (* 如果有卡片丢弃, 则直接丢弃, 否则判断是否可拿 *)
                           (case held_list of
                              card1::held_list_rst => helper(card_list, held_list_rst, move_list @ [Discard card1], sum - card_value(card1))
                            | [] => 
                                 (* 如果超过goal, 则不拿, 否则拿 *)
                                 (if sum + card_value(card) > goal
                                 then
                                    move_list
                                 else
                                    helper(card_list_rst, [card], move_list @ [Draw], sum + card_value(card))))
                      | SOME card2 => move_list @ [Discard card2, Draw]
               end
   in
     helper(card_list, [], [], 0)
   end

测试

(* https://www.coursera.org/learn/programming-languages/discussions/forums/c6s06Cj0Eea7jBLLHPwd0w/threads/tyxdzFi9Eeap3RLIDpHs4Q *)
(* All tests should evaluate to true. *)

(* NOTE: None of the tests in this test suite verify that your solutions *)
(* use pattern matching instead of list / option functions. *)
(* You should check that yourself! *)

(** 38 Cons Cells **)

(* NOTE: These tests do not verify that your solution is tail-recursive. *)
(* You should check that yourself! *)
use "hw2_extra.sml";

val test_length_of_a_list_1 = length_of_a_list [1] = 1
val test_length_of_a_list_2 = length_of_a_list [] = 0
val test_length_of_a_list_3 = length_of_a_list [[], [], [1, 2]] = 3
val test_length_of_a_list_4 = length_of_a_list [(1, "hi"), (2, "there")] = 2
val test_length_of_a_list_5 = length_of_a_list ["a", "quick", "brown", "fox"] = 4

(** Pass/Fail **)

(* Pass/Fail -- 1 *)
val test_pass_or_fail_1 = pass_or_fail { id = 1023, grade = SOME 73 } = fail
val test_pass_or_fail_2 = pass_or_fail { id = 1, grade = SOME 48 } = fail
val test_pass_or_fail_3 = pass_or_fail { id = 10231023, grade = SOME 0 } = fail
val test_pass_or_fail_4 = pass_or_fail { id = 1729, grade = NONE } = fail
val test_pass_or_fail_5 = pass_or_fail { id = 432, grade = SOME 74 } = fail
val test_pass_or_fail_6 = pass_or_fail { id = 2, grade = SOME 75 } = pass
val test_pass_or_fail_7 = pass_or_fail { id = 13, grade = SOME 100 } = pass
val test_pass_or_fail_8 = pass_or_fail { id = 15, grade = SOME 86 } = pass

(* Pass/Fail -- 2 *)
val test_has_passed_1 = has_passed { id = 1023, grade = SOME 73 } = false
val test_has_passed_2 = has_passed { id = 1, grade = SOME 48 } = false
val test_has_passed_3 = has_passed { id = 10231023, grade = SOME 0 } = false
val test_has_passed_4 = has_passed { id = 1729, grade = NONE } = false
val test_has_passed_5 = has_passed { id = 432, grade = SOME 74 } = false
val test_has_passed_6 = has_passed { id = 2, grade = SOME 75 } = true
val test_has_passed_7 = has_passed { id = 13, grade = SOME 100 } = true
val test_has_passed_8 = has_passed { id = 15, grade = SOME 86 } = true

(* Pass/Fail -- 3 *)
val test_number_passed_1 = number_passed [{ id = 1, grade = SOME 65 }, { id = 2, grade = SOME 82 },
    { id = 3, grade = NONE }, { id = 5, grade = SOME 96 }] = 2
val test_number_passed_2 = number_passed [] = 0
val test_number_passed_3 = number_passed [{ id = 12, grade = SOME 100 }, { id = 14, grade = SOME 0 },
    { id = 9, grade = NONE }, { id = 2, grade = NONE }] = 1
val test_number_passed_4 = number_passed [{ id = 1, grade = SOME 76 }, { id = 2, grade = SOME 82 },
    { id = 5, grade = SOME 96 }] = 3

(* Pass/Fail -- 4 *)
val test_number_misgraded_1 = number_misgraded [(pass, { id = 2, grade = SOME 75 }), (pass, { id = 1023, grade = SOME 73 }), (fail, { id = 1, grade = SOME 48 })] = 1
val test_number_misgraded_2 = number_misgraded [(pass, { id = 2, grade = SOME 75 }), (pass, { id = 1023, grade = SOME 73 }), (pass, { id = 15, grade = SOME 86 })] = 1
val test_number_misgraded_3 = number_misgraded [(fail, { id = 2, grade = SOME 75 }), (fail, { id = 1023, grade = SOME 73 }), (fail, { id = 1, grade = SOME 48 })] = 1
val test_number_misgraded_4 = number_misgraded [] = 0

(** Forest For The Trees **)

(* Forest For The Trees -- 1 *)
val test_tree_height_1 = tree_height (node { value = 0, left = node { value = 0,
    left = node { value = 0, left = leaf, right = leaf }, right = leaf },
    right = node { value = 0, left = leaf, right = leaf } }) = 3
val test_tree_height_2 = tree_height leaf = 0
val test_tree_height_3 = tree_height (node { value = "abcde", left = leaf, right = leaf }) = 1
val test_tree_height_4 = tree_height (node { value = true, left = leaf, right = leaf }) = 1
val test_tree_height_5 = tree_height (node { value = 0, left = leaf,
    right = node { value = 0, left = node { value = 0, left = leaf, right = leaf }, right = leaf } }) = 3

(* Forest For The Trees -- 2 *)
val test_sum_tree_1 = sum_tree (node { value = 1, left = node { value = 2,
    left = node { value = 3, left = leaf, right = leaf }, right = leaf },
    right = node { value = 4, left = leaf, right = leaf } }) = 10
val test_sum_tree_2 = sum_tree leaf = 0
val test_sum_tree_3 = sum_tree (node { value = 1729, left = leaf, right = leaf }) = 1729
val test_sum_tree_4 = sum_tree (node { value = 32, left = leaf,
    right = node { value = ~60, left = node { value = 17, left = leaf, right = leaf }, right = leaf } }) = ~11

(* Forest For The Trees -- 3 *)
val test_gardener_1 = gardener (node { value = leave_me_alone,
    left = node { value = prune_me, left = node { value = leave_me_alone, left = leaf, right = leaf }, right = leaf },
    right = node { value = leave_me_alone, left = leaf, right = leaf } }) =
    node { value = leave_me_alone, left = leaf, right = node { value = leave_me_alone, left = leaf, right = leaf } }
val test_gardener_2 = gardener leaf = leaf
val test_gardener_3 = gardener (node { value = prune_me, left = node { value = prune_me,
    left = node { value = leave_me_alone, left = leaf, right = leaf }, right = leaf },
    right = node { value = leave_me_alone, left = leaf, right = leaf } }) = leaf
val test_gardener_4 = gardener (node { value = leave_me_alone,
    left = node { value = leave_me_alone, left = node { value = leave_me_alone, left = leaf, right = leaf }, right = leaf },
    right = node { value = leave_me_alone, left = leaf, right = leaf } }) =
    node { value = leave_me_alone, left = node { value = leave_me_alone,
    left = node { value = leave_me_alone, left = leaf, right = leaf }, right = leaf },
    right = node { value = leave_me_alone, left = leaf, right = leaf } }
val test_gardener_5 = gardener (node { value = leave_me_alone, left = node { value = leave_me_alone,
    left = node { value = prune_me, left = leaf, right = leaf }, right = leaf },
    right = node { value = prune_me, left = leaf, right = leaf } }) =
    node { value = leave_me_alone, left = node { value = leave_me_alone, left = leaf, right = leaf }, right = leaf }
val test_gardener_6 = gardener (node { value = prune_me, left = leaf, right = leaf }) = leaf
val test_gardener_7 = gardener (node { value = leave_me_alone, left = leaf, right = leaf }) =
    node { value = leave_me_alone, left = leaf, right = leaf }
val test_gardener_8 = gardener (node { value = leave_me_alone, left = leaf,
    right = node { value = prune_me, left = node { value = prune_me, left = leaf, right = leaf }, right = leaf } }) =
    node { value = leave_me_alone, left = leaf, right = leaf }

(* problem 8 *)
val test_problem8_1 = last([3]) = 3
val test_problem8_2 = last([1, 2, 5]) = 5

(* problem 9 *)
val test_problem9_1 = is_positive(ZERO) = false
val test_problem9_2 = is_positive(SUCC ZERO) = true
val test_problem9_3 = is_positive(SUCC (SUCC ZERO)) = true

(* problem 10 *)
(* val test_problem10_1 = pred(ZERO) = Negative *)
val test_problem10_2 = pred(SUCC ZERO) = ZERO
val test_problem10_3 = pred(SUCC (SUCC ZERO)) = (SUCC ZERO)

(* problem 11 *)
val test_problem11_1 = nat_to_int(ZERO) = 0
val test_problem11_2 = nat_to_int(SUCC ZERO) = 1
val test_problem11_3 = nat_to_int(SUCC (SUCC ZERO)) = 2

(* problem 12 *)
val test_problem12_1 = int_to_nat(0) = ZERO
val test_problem12_2 = int_to_nat(1) = SUCC ZERO
val test_problem12_3 = int_to_nat(2) = SUCC (SUCC ZERO)

(* problem 13 *)
val test_problem13_1 = add(ZERO, SUCC ZERO) = SUCC ZERO
val test_problem13_2 = add(SUCC ZERO, SUCC ZERO) = SUCC (SUCC ZERO)
val test_problem13_3 = add(SUCC ZERO, SUCC (SUCC ZERO)) = SUCC (SUCC (SUCC ZERO))

(* problem 14 *)
val test_problem14_1 = sub(SUCC ZERO, ZERO) = SUCC ZERO
val test_problem14_2 = sub(SUCC (SUCC ZERO), SUCC ZERO) = (SUCC ZERO)
val test_problem14_3 = sub(SUCC (SUCC (SUCC ZERO)), SUCC (SUCC (SUCC ZERO))) = ZERO

(* problem 15 *)
val test_problem15_1 = mult(SUCC ZERO, ZERO) = ZERO
val test_problem15_2 = mult(SUCC (SUCC ZERO), SUCC ZERO) = SUCC (SUCC ZERO)
val test_problem15_3 = mult(SUCC (SUCC ZERO), SUCC (SUCC ZERO)) = SUCC (SUCC (SUCC (SUCC ZERO)))

(* problem 16 *)
val test_problem16_1 = less_than(SUCC ZERO, ZERO) = false
val test_problem16_2 = less_than(SUCC (SUCC ZERO), SUCC ZERO) = false
val test_problem16_3 = less_than(SUCC (SUCC ZERO), SUCC (SUCC ZERO)) = false
val test_problem16_4 = less_than(ZERO, SUCC ZERO) = true
val test_problem16_5 = less_than(SUCC ZERO, SUCC (SUCC ZERO)) = true

(* https://gist.github.com/gfizz/11ba5ee6e1534d9a5bb8d2e6f682b0e2 *)
(* problem 17 *)
val test_problem17_1 = isEmpty(Elems[]) = true
val test_problem17_2 = isEmpty(Elems[0]) = false
val test_problem17_3 = isEmpty(Range{from=3, to=2}) = true
val test_problem17_4 = isEmpty(Range{from=0, to=0}) = false
val test_problem17_5 = isEmpty(Range{from=(~5), to=(~4)}) = false
val test_problem17_6 = isEmpty(Union(Elems[], Range{from=9, to=6})) = true
val test_problem17_7 = isEmpty(Union(Union(Elems[], Range{from=6, to=9}), Elems[])) = false
val test_problem17_8 = isEmpty(Union(Union(Range{from=1, to=(~1)}, Elems[]), Union(Elems[], Elems[]))) = true
val test_problem17_9 = isEmpty(Intersection(Elems[], Elems[])) = true
val test_problem17_10 = isEmpty(Intersection(Elems[1, 2], Elems[])) = true
val test_problem17_11 = isEmpty(Intersection(Range{from=9, to=6}, Elems[42])) = true
val test_problem17_12 = isEmpty(Intersection(Elems[0], Range{from=1, to=2})) = true
val test_problem17_13 = isEmpty(Intersection(Union(Elems[], Elems[42]), Elems[42, 41])) = false
val test_problem17_14 = isEmpty(Intersection(Elems[0], Union(Elems[], Union(Range{from=9, to=6}, Intersection(Elems[1], Elems[0]))))) = true
val test_problem17_15 = isEmpty(Union(Intersection(Elems[], Elems[]), Intersection(Elems[2, 0], Elems[1]))) = true
val test_problem17_16 = isEmpty(Intersection(Intersection(Elems[1, 3, 5], Elems[2, 3, 6]), Elems[1, 2, 4])) = true
val test_problem17_17 = isEmpty(Intersection(Intersection(Elems[1, 3, 5], Elems[2, 3, 6]), Elems[1, 3, 4])) = false
val test_problem17_18 = isEmpty(Intersection(Intersection(Elems[1, 2], Elems[2, 3]), Intersection(Elems[2, 3], Elems[3, 4]))) = true
val test_problem17_19 = isEmpty(Intersection(Union(Elems[1], Elems[2]), Intersection(Elems[1], Elems[1]))) = false
val test_problem17_20 = isEmpty(Intersection(Union(Elems[1], Elems[2]), Intersection(Elems[2], Elems[2]))) = false
val test_problem17_21 = isEmpty(Intersection(Union(Elems[1], Elems[2]), Intersection(Elems[3], Elems[3]))) = true
val test_problem17_22 = isEmpty(Intersection(Intersection(Elems[1], Elems[1]), Union(Elems[1], Elems[2]))) = false
val test_problem17_23 = isEmpty(Intersection(Intersection(Elems[2], Elems[2]), Union(Elems[1], Elems[2]))) = false
val test_problem17_24 = isEmpty(Intersection(Intersection(Elems[3], Elems[3]), Union(Elems[1], Elems[2]))) = true

(* problem 18 *)
val test_problem18_1 = contains(Elems[], 0) = false
val test_problem18_2 = contains(Elems[42], 42) = true
val test_problem18_3 = contains(Elems[1, 3, 5, 7, 9], 7) = true
val test_problem18_4 = contains(Elems[~1, 1], 0) = false
val test_problem18_5 = contains(Range{from=3, to=2}, 3) = false
val test_problem18_6 = contains(Range{from=(~5), to=(~5)}, (~5)) = true
val test_problem18_7 = contains(Range{from=1, to=6}, 0) = false
val test_problem18_8 = contains(Union(Elems[1], Range{from=2, to=8}), 9) = false
val test_problem18_9 = contains(Union(Elems[42], Elems[42, 33]), 33) = true
val test_problem18_10 = contains(Union(Range{from=0, to=5}, Union(Elems[~3, ~1, 1], Elems[])), ~1) = true
val test_problem18_11 = contains(Intersection(Elems[1, 2, 3], Elems[2, 3, 4]), 1) = false
val test_problem18_12 = contains(Intersection(Elems[~1, ~2], Elems[0, ~2]), ~2) = true
val test_problem18_13 = contains(Intersection(Elems[1, 2, 3], Intersection(Elems[0, 2, 1], Elems[3, 1, 5])), 1) = true
val test_problem18_14 = contains(Intersection(Union(Elems[1], Elems[2, 3]), Elems[3, 0]), 2) = false
val test_problem18_15 = contains(Union(Intersection(Elems[0, 2, 4], Range{from=0, to=2}), Intersection(Elems[0, 4], Elems[0, 2])), 4) = false

(* problem 19 *)
fun insert(xs, x) =
    case xs of
            [] => [x]
        | x'::xs' => if x <= x' then x::xs else x'::insert(xs', x)
fun sort(xs) =
    case xs of
            [] => []
        | x::xs' => insert(sort(xs'), x)

val test_problem19_1 = toList(Elems[]) = []
val test_problem19_2 = toList(Elems[0, 0, 0]) = [0]
val test_problem19_3 = sort(toList(Elems[3, 1, 2])) = [1, 2, 3]
val test_problem19_4 = sort(toList(Elems[0, 5, ~1, 4, 3, 3, 0, ~1, 5, 2, 1, 4])) = [~1, 0, 1, 2, 3, 4, 5]
val test_problem19_5 = sort(toList(Range{from=1, to=0})) = []
val test_problem19_6 = sort(toList(Range{from=0, to=1})) = [0, 1]
val test_problem19_7 = sort(toList(Range{from=42, to=42})) = [42]
val test_problem19_8 = sort(toList(Range{from=(~2), to=2})) = [~2, ~1, 0, 1, 2]
val test_problem19_9 = sort(toList(Union(Elems[], Range{from=1, to=0}))) =[]
val test_problem19_10 = sort(toList(Union(Elems[3, 3, 0, 2, 0], Range{from=3, to=5}))) = [0, 2, 3, 4, 5]
val test_problem19_12 = sort(toList(Union(Union(Elems[1, 2, 1], Elems[3, 2, 4]), Union(Range{from=7, to=7}, Range{from=6, to=5})))) = [1, 2, 3, 4, 7]
val test_problem19_13 = sort(toList(Intersection(Range{from=1, to=0}, Elems[]))) = []
val test_problem19_14 = sort(toList(Intersection(Elems[5, 3, 1, ~1], Elems[1, 2, 3, 2, 1]))) = [1, 3]
val test_problem19_15 = sort(toList(Union(Union(Elems[7], Elems[0]), Intersection(Range{from=5, to=9}, Union(Elems[6], Elems[]))))) = [0, 6, 7]
val test_problem19_16 = sort(toList(Intersection(Union(Intersection(Range{from=1, to=5}, Range{from=3, to=7}), Elems[6]), Elems[4, 2, 6]))) = [4, 6]