Hey I'm just getting started with Haskell and I'm trying to write a programm that translates an input word by reading a .txt file that has some translations in it. Before that I've written a programm that lets a user put in translations.
import System.IO
main :: IO ()
main = do
putStrLn "Type in an english word"
d <- getLine
if null d
then return ()
else do
putStrLn "Type in a german translation"
b <- getLine
putStrLn (d ++ " means in german " ++ b)
appendFile "woerterbuch.txt" (", " ++ d ++ " " ++ b)
main
So now I've got the file woerterbuch.txt in which I have the english and german words listed like this: Hello Hallo, House Haus, ect. What I'm trying to do now is to get these words into Tuples so I can print the second (german) word if the user types in an english word.
import System.IO
main :: IO()
main = do
putStrLn "Type in an english word"
d <- getLine
[contents] <- readFile "woerterbuch.txt"
if d `elem` [contents]
then do
print contents
else do
putStrLn "There's no translation available!"
This is what I've come up with after multiple tries, I understand that elem
is for Lists so I need an alternative for my .txt file and then try to put out the second word of the tuple with the snd
command.
So firstly how can I create the translations input as tuples and secondly how can I test if the asked word is in this list and then print out the second element of the tuple it is part of.
Any help would be greatly appreciated.