Question

i'm trying to right a simple function to sort a list using a selection sort, Im code is below:

fun slctsrt [] = []
 |  slctsrt (x::xs) = slctsrt2 (xs, x, []);
fun slctsrt2 ([], min, []) = min
 |  slctsrt2 ([], min, y::ys) = min :: slctsrt2 (ys, y, [])
 |  slctsrt2 (x::xs, min, ys) = if x<min then slctsrt2 (xs, x, min::ys)
                          else slctsrt2 (xs,min,x::ys);

... and it returns the error below:

"lctsrt.txt", line 7, characters 32-35:
!  |  slctsrt2 ([], min, y::ys) = min :: slctsrt2 (ys, y, [])
!                                 ^^^
! Type clash: expression of type
!   'a list
! cannot have type
!   'a
! because of circularity
Was it helpful?

Solution

The function slctsrt2 should return a list. However, the first clause :

fun slctsrt2 ([], min, []) = min

returns a 'a.

It should be:

 fun slctsrt2 ([], min, []) = min :: []
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top