3

I'm running into an odd issue with a small function written in Haskell which is meant to go through a string and replace a word within it with a different word. The function works when a small string is used, but when searching through large strings to replace the words strange things happen. The function is defined as follows:

swapwords :: String -> String -> String -> String
swapwords w1 w2 [] = []
swapwords w1 w2 (x:xs)
    | length (x:xs) < n = (x:xs)
    | otherwise = do     
    if w1 == take n (x:xs) then w2 ++ swapwords w1 w2 (drop n (x:xs))
        else x:swapwords w1 w2 xs
    where n = length w1 

When running with swapwords "ab" "ba" "ab" the output is correctly "ba", however when ran with the input "lamb" "buffalo" "Mary had a little lamb whose fleece was white as snow" the output still correctly swaps "lamb" with "buffalo", however added onto the end of the string is part of the messages first displayed when WinHugs is loaded, e.g "Mary had a little buffalo, whose fleece was white as snown -98 to enable exensions

Type :? for help"

As a relative notice to Haskell I have no idea why this is happening and would appreciate any input that could be given as to why this is occuring.

3
  • Submit a bug report to Hugs, it works correctly on ghc Commented Nov 27, 2013 at 23:44
  • 1
    Could you post the hugs session verbatim? I find it pretty unlikely that the output string is wrong, I think it's probably an I/O buffering issue or something. One way to check would be to test the length of the output and see if it's the length you expect. Commented Nov 27, 2013 at 23:50
  • Thanks for the comments, I shall submit a bug report and swap to using GHC Commented Nov 28, 2013 at 2:12

1 Answer 1

3

I ran you code in GHCi and worked perfectly. Maybe a bug in WinHugs?

You should consider moving to GHC. WinHugs isn't updated in years.

Sign up to request clarification or add additional context in comments.

1 Comment

Ran it in GHCi as well, it works fine for that input, and generalized to Eq a => [a] -> [a] -> [a] -> [a], it works for swapwords [1, 2, 3] [3, 2, 1] [1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5]. Sounds like a WinHugs problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.