Why is this Haskell code so slow?
<p>I'm kind of new to Haskell and tried making a scrabble solver. It takes in the letters you currently have, finds all permutations of them and filters out those that are dictionary words. The code's pretty simple: </p>

<pre><code>import Data.List

main = do
    dict    &lt;- readFile "words"
    letters &lt;- getLine
    let dictWords = words dict
    let perms = permutations letters
    print [x | x &lt;- perms, x `elem` dictWords]
</code></pre>

<p>However it's incredibly slow, compared to a very similar implementation I have with Python. Is there something fundamental I'm doing wrong?</p>

<p>*edit: Here's my Python code:</p>

<pre><code>from itertools import permutations

letters = raw_input("please enter your letters (without spaces): ")

d = open('words')
dictionary = [line.rstrip('\n') for line in d.readlines()]
d.close()

perms = ["".join(p) for p in permutations(letters)]

validWords = []

for p in perms:
    if p in dictionary: validWords.append(p)


for validWord in validWords:
    print validWord
</code></pre>

<p>I didn't time them precisely, but roughly it feels like the Python implementation is about 2x as fast as the Haskell one. Perhaps I should't have said the Haskell code was "incredibly slow" in comparison, but since Haskell is statically typed I guess I just thought that it should've been much faster, and not slower than Python at all.</p>

<blockquote>
  <p>I'm kind of new to Haskell and tried making a scrabble solver.</p>
</blockquote>

<p>You can substantially improve things by using a better algorithm.</p>

<p>Instead of testing every permutation of the input letters, if you
sort them first you can make only one dictionary lookup and get
all of the possible words (anagrams) which may be formed from 
them (using all of them).</p>

<p>Here is code which creates that dictionary as a Data.Map.
There is a start-up cost to creating the Map, but after
the first query subsequent lookups are very fast.</p>

<pre><code>import Data.List
import qualified Data.Map.Strict as Map
import Control.Monad
import System.IO

main = do
  contents &lt;- readFile "words"
  let pairs = [ (sort w, [w]) | w &lt;- words contents ]
      dict = foldl' (\m (k,v) -&gt; Map.insertWith (++) k v m) Map.empty pairs
      -- dict = foldr (\(k,v) m -&gt; Map.insertWith (++) k v m) Map.empty pairs
  forever $ do
    putStr "Enter letters: " &gt;&gt; hFlush stdout
    letters &lt;- getLine
    case Map.lookup (sort letters) dict of
      Nothing -&gt; putStrLn "No words."
      Just ws -&gt; putStrLn $ "Words: " ++ show ws
</code></pre>

<p>Map creation time for a word file of 236K words (2.5 MB) is about 4-5 seconds. Better performance is likely possible by using ByteStrings or Text instead of Strings.</p>

<p>Some good letter combinations to try:</p>

<pre><code>steer rat tuna lapse groan neat
</code></pre>

<p>Note: Using GHC 7.10.2 I found this code performed the best <em>without</em> compiling with -O2.</p>

python haskell optimization language-comparisons