0
votes

I am using the following code in Haskell to get an output of letter combinations.

   combinations pre suf letters = prefixed ++ suffixed   
    where
     perms = permutation letters
     prefixed = map (\x -> pre ++ x) $ perms
     suffixed = map (\x -> x ++ suf) $ perms

I want to import a large textfile like dictonary.txt as a list ["Apple", "Banana.....]" with the structure of:

Apple
Banana
Stawberry
...... and so on

This imported [String] I want to merge with the output [String] of combinations. Somebody who can help me with this?

edit: To make it more clear. The combinations function gives a output like:

["banana","nanaba,..."] <- All posible combinations of a couple of characters.

I want to merge this list with a list with a list that is created from a txt file (but I dont know ho to import a txt to a list of strings and use it to merge). So the output will be like.

["banana","nanaba,...,Apple,Banana,Strawbery"] 

After that it will print the double words in the string.

Another edit with Ankurs code:

combinations pre suf letters = prefixed ++ suffixed
  where
    perms = permutation letters
    prefixed = map (\x -> pre ++ x)  $ perms
    suffixed = map (\x -> x ++ suf)  $ perms

fileLines :: FilePath -> IO [String]
fileLines file = readFile file >>= (\x -> return $ lines x)
        main = do 
         lines <- fileLines "directory.txt"
         putStr $ (combinations pre suf letters) ++ lines

I will get a parser error on this one! :22:22: parse error on input `='

Can someone help me how to order this code? So it dont get anny errors?

2
Right now the question is still a little unclear. Could you give a small sample of the output that you expect?Gabriella Gonzalez
How do you want to merge them?Gabe
I guess by merge u mean concatAnkur
You have indented main, that must be non-indented.Daniel Fischer
Well, yes. When you call combinations, you must provide the actual arguments. Something like putStr . unlines $ combinations "prefix" "suffix" "banana" ++ lines.Daniel Fischer

2 Answers

1
votes
fileLines :: FilePath -> IO [String]
fileLines file = readFile file >>= (\x -> return $ lines x)


main = do
           lines <- fileLines "directory.txt"
           putStr $ show $ (combinations pre suf letters) ++ lines
1
votes

Ok, so first off, let me clean up your combinations function a little bit:

import Data.List (permutations)

-- Please add type signatures when you ask questions
combinations :: [a] -> [a] -> [a] -> [[a]]
combinations pre suf letters = prefixed ++ suffixed   
  where
    perms = permutations letters
    prefixed = map (pre ++) perms
    suffixed = map (++ suf) perms

There's no need for the $ and you can use operator sectioning to avoid writing out the lambda.

Next up, your main function needs to read in the lines from the file. This is as simple as:

main = do
    ls <- fmap lines (readFile "someFile.txt")
    ...

Now let's say that you pass your combination function the following arguments:

combinations "foo" "bar" "banana" :: [String]

That leaves you with the following main program:

main = do
    ls1 <- fmap lines (readFile "someFile.txt")
    let ls2 = combinations "foo" "bar" "banana"
    ...

Now you need to figure out how to combine ls1 and ls2 into a single list of lines and then print them out or do whatever it is you need to do with that list of lines.