Explore our Solution Library

Home  »  Solution Library  »  Help me with my Scala Programming Assignment  »  Number of Views - 679

Help me with my Scala Programming Assignment

Question - [1] def merge_sort(list: List[Int], cmp:(Int,Int)=>Boolean): List[Int] = { if (l1 == Nil) l2 else if (l2 == Nil) l1 else if (cmp(l1.head, l2.head)) l1.head :: merge(l1.tail, l2) else l2.head :: merge(l1, l2.tail) } list } else { val lngth = list.length val mid = lngth / 2 val a = list.slice(0, mid) val b = list.slice(mid, lngth) val sorted1 = merge_sort(a, cmp) val sorted2 = merge_sort(b, cmp) merge(sorted1, sorted2) } } val lte:(Int,Int)=>Boolean = _ <= _ println(merge_sort(List[Int](73,35,45,21,13), lte)) } } 1. Consider the scala program at [1]. It uses a merge sort to sort the integer list 73, 35, 45, 21, ...Read More

Solution Preview - ist (13, 21, 35, 45, 73) - File name - Sort.scala Boolean - Either true or false Anonymous cmp is coming from its parent function "merge_sort", function "merge" is local for "merge_sort" and cmp is also local for "merge_sort". Therefore merge has access to use the cmp. A test code has also been written to prove this. (File name - Test.scala) 2)

Original Question Documents

Email your assignment/project

enquiry@allassignmenthelp.com

Want to place an
order on the call?
It's free