0

I am new to SML.I got this sorting algo to implement where in each iteration,I have to pick minimum element from the list, remove it and create sorted list.

I did below coding to solve the problem.

I wrote 2 helper functions to pickup minimum element from the list and remove one element from the list.

fun minList(x::xs) =List.foldl (fn (x,y)=> if x<y then x else y) x (x::xs);

fun remElem(x, l) =
   case l of
   [] => []
 | (x1::x2::xs) => if x1=x then (x2::xs) else (x1::xs)
;

Above two programs ran successfully.

Below is my sorting code.

fun simpSort(xs)=
let fun aux(xs,acc)=
       case xs of
            [] =>acc
           | [x] => [x]
            | (x::xs) => let val m = minList(xs) 
                       in
                     aux(remElem(m,xs),acc@[m])
                       end
in aux(xs,[])
end;

This sorting program is giving error.

  • simpSort([3,1]);

    uncaught exception Match [nonexhaustive match failure] raised at: stdIn:433.59

Please advise.

Sri
  • 99
  • 10
  • `remElem` only handles the empty list and lists with at least two elements. – molbdnilo Mar 30 '19 at 11:57
  • Also, `remElem` removes either the first or the second element of the list, and nothing else. That's probably not what you intended. – molbdnilo Mar 30 '19 at 12:01
  • Thanks. When I do this code - fun remElem (i, xs) = case xs of []=>[] | x::xs => if i = x then remElem(i,xs) else x::remElem(i,xs) It is deleting all occurences of the input char where I want to delete only the first occurence. – Sri Mar 30 '19 at 14:30
  • 1
    I wasn't clear enough; you're removing the first element if it is the first occurrence, otherwise you're removing the second element – regardless of what that second element is. For instance, `remElem(97, [1,2,97])` will produce `[1,97]`, and so will `remElem(12, [1,2,97])`. – molbdnilo Mar 30 '19 at 14:58
  • Thanks.I modified the remElem program.It is now fun remElem(x, l) = case l of [] => [] | [x] => [] | (x1::xs) => if x1=x then (xs) else x1::remElem(x,xs) ; It is giving proper output. But sorting function SimpSort is still giving erroneous result. – Sri Mar 30 '19 at 19:22
  • I managed to fix it.I noticed I was not passing arguments properly in helper functions.Thanks for the help. – Sri Mar 30 '19 at 19:34

1 Answers1

0

Since you've solved your problem, here are some hints for improving a working version of your code:

  • Find the minimum of a list in a way that supports empty lists:

    fun minimum [] = NONE
      | minimum (x::xs) = SOME (foldl Int.min x xs)
    
  • Simplify pattern matching in the function that removes the first occurrence of an element from a list:

    fun remove (_, []) = []
      | remove (y, x::xs) =
        if x = y
        then xs
        else x :: remove (y, xs)
    
  • Use those in combination to write simpSort:

    fun simpSort xs =
        case minimum xs of
             NONE => []
           | SOME x => x :: simpSort (remove (x, xs))
    

I shouldn't have to say that this sorting algorithm is terribly inefficient. :-P

sshine
  • 15,635
  • 1
  • 41
  • 66