I write a code to sort index of tuple list I try to use map with bubblesort()
to avoid using loops
bubblesort::(Ord t) => [t]->[t]
bubblesort[x,y,z,xs]=
if x<y then x : map bubblesort [y,z,xs]
else y : map bubblesort [x,z,xs]
but it give me an error that :
ERROR line 20 - Type error in list *** Expression : [y,xs] *** Term : xs *** Type : [a] *** Does not match : a *** Because : unification would give infinite type
*note please give me instruction only
–The complier is online compiler
The main problem you’re having is one which I see beginners make all the time. You’re using a list, so you’ve decided that you have to use square brackets. What you really need is something like:
when you use something like
You’re explicitly pattern matching on a list with exactly three elements. If you use
then you’re explicitly matching on a list with at least three elements, where the first is bound to the name a, the second b, the third c, and the rest of the list (no matter how long it is) is called xs.
I hope that helps clear it up. It’s a very common mistake.