I got a little problem with my haskell code:
import Prelude
hangman :: String -> IO ()
hangman [] = return ()
hangman s = do putStr ("Secret: " ++ text s ++ "\n")
putStr "Enter a character: "
charr <- getChar
function s charr (text s)
function :: String -> Char -> String -> IO ()
function [] _ _ = return ()
function (a:bc) charr (g:jt) = do putStr ("Secret: " ++ (textBack (a:bc) charr (g:jt)) ++ "\n")
putStr "Enter a character: "
charbaby <- getChar
putStr "\n"
function (a:bc) charbaby (textBack (a:bc) charbaby (g:jt))
text :: String -> String
text [] = ""
text (x:ys) = "*" ++ text ys
textBack :: String -> Char -> String -> String
textBack [] _ _ = ""
textBack (a:bc) charr (g:jt) = if charr == a
then charr : textBack bc charr jt
else g : textBack bc charr jt
And here is the output:
*Main> hangman "hello"
Secret: *****
Enter a character: h
Secret: h****
Enter a character:
Secret: h****
Enter a character: e
Secret: he***
Enter a character:
Secret: he***
Enter a character: l
Secret: hell*
Enter a character:
Secret: hell*
Enter a character: o
Secret: hello
Enter a character:
Secret: hello
Enter a character:
And as you can see there are some lines which are called multiple times and I dont know why. I hope you can help me :)
Thank you,
Finn