Skip to content

Commit

Permalink
finished exercises for Course.Anagrams and Course.FastAnagrams
Browse files Browse the repository at this point in the history
  • Loading branch information
miladz68 committed Jan 3, 2017
1 parent 1217782 commit 5240545
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ After this, the following progression of modules is recommended:
* `Course.MoreParser` -> Done
* `Course.JsonParser` -> Done
* `Course.Interactive` -> Done
* `Course.Anagrams`
* `Course.FastAnagrams`
* `Course.Anagrams` -> Done
* `Course.FastAnagrams` -> Done
* `Course.Cheque`
16 changes: 12 additions & 4 deletions src/Course/Anagrams.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ module Course.Anagrams where
import Course.Core
import Course.List
import Course.Functor
import Course.Monad

import Course.Applicative
import Course.Traversable
import Course.Optional
{-
Functions you will need
Expand All @@ -32,13 +36,17 @@ anagrams ::
Chars
-> Filename
-> IO (List Chars)
anagrams =
error "todo: Course.Anagrams#anagrams"
anagrams ls flName = do
content <- readFile flName
return $ intersectBy equalIgnoringCase (words content) (permutations ls)


-- Compare two strings for equality, ignoring case
equalIgnoringCase ::
Chars
-> Chars
-> Bool
equalIgnoringCase =
error "todo: Course.Anagrams#equalIgnoringCase"
equalIgnoringCase Nil Nil = True
equalIgnoringCase _ Nil = False
equalIgnoringCase Nil _ = False
equalIgnoringCase (a:.as) (b:.bs) = (toLower a == toLower b) && equalIgnoringCase as bs
12 changes: 10 additions & 2 deletions src/Course/FastAnagrams.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@ import Course.List
import Course.Functor
import qualified Data.Set as S

import Course.Monad

import Course.Applicative
import Course.Traversable
import Course.Optional

-- Return all anagrams of the given string
-- that appear in the given dictionary file.
fastAnagrams ::
Chars
-> Filename
-> IO (List Chars)
fastAnagrams =
error "todo: Course.FastAnagrams#fastAnagrams"
fastAnagrams ls flName = do
content <- readFile flName
let dicSet = S.fromList (hlist (words content))
return $ foldLeft (\acc x -> if x `S.member` dicSet then x:.acc else acc) Nil (permutations ls)

newtype NoCaseString =
NoCaseString {
Expand Down

0 comments on commit 5240545

Please sign in to comment.