DiVeRsi0n
DiVeRsi0n

Reputation: 51

"ERROR - C stack overflow" in Haskell sorting Strings

I have an "ERROR - C stack overflow" when I'm using my sort method in a where clause. But if I call the sort function separately (sort "aString") it works. Any suggestions?

function :: String->String->Bool        
function w1 w2 
    |w1==w2     = True
    |otherwise  = False
    where (w1,w2) = (sort w1,sort w2)

Upvotes: 0

Views: 69

Answers (1)

sepp2k
sepp2k

Reputation: 370425

where (w1,w2) = (sort w1,sort w2)

This definition is infinitely recursive (w1 = sort w1 = sort (sort w1) = ...). If you want to refer to the old bindings of w1 and w2 in your definition, you need to give your new bindings different names (and then use those new names when referring to the new values).

Something like this:

function :: String->String->Bool        
function w1 w2 
    |sortedW1 == sortedW2 = True
    |otherwise = False
    where (sortedW1, sortedW2) = (sort w1,sort w2)

Or of course just:

function :: String->String->Bool        
function w1 w2 
    |sort w1 == sort w2 = True
    |otherwise = False

Or even:

function :: String -> String -> Bool        
function w1 w2 = sort w1 == sort w2

Upvotes: 4

Related Questions