-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnagrams.hs
38 lines (30 loc) · 996 Bytes
/
Anagrams.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Anagrams where
import Core
import qualified Data.Char as Ch
import qualified Data.Function as Fn
import qualified Functor as F
import List (Chars, FilePath, List (..))
import qualified List as L
{-
Functions you will need
--
\* fmap :: (a -> b) -> IO a -> IO b
\* readFile :: FilePath -> IO Str
\* lines :: Str -> [Str]
\* permutations :: [a] -> [[a]]
\* intersectBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
\* toLower :: Char -> Char
Functions that might help
-
\* on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
-}
-- Return all anagrams of the given string
-- that appear in the given dictionary file.
anagrams :: Chars -> FilePath -> IO (List Chars)
anagrams s filename =
L.intersectBy equalIgnoringCase (L.permutations s) . L.lines F.<$> L.readFile filename
-- Compare two strings for equality, ignoring case
equalIgnoringCase :: Chars -> Chars -> Bool
equalIgnoringCase = Fn.on (==) (Ch.toLower F.<$>)