Skip to content

Commit

Permalink
Complete course
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhijit Sarkar committed Jan 3, 2025
1 parent 7c0f2f6 commit c08fcd6
Show file tree
Hide file tree
Showing 8 changed files with 852 additions and 5 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ After this, we recommend the following progression of modules:
* [MoreParser](src/MoreParser.hs)
* [JsonParser](src/JsonParser.hs)
* [Alternative](src/Alternative.hs)
* Interactive
* Anagrams
* FastAnagrams
* Cheque
* [Interactive](src/Interactive.hs)
* [Anagrams](src/Anagrams.hs)
* [FastAnagrams](src/FastAnagrams.hs)
* [Cheque](src/Cheque.hs)

## Running tests

Expand Down
5 changes: 5 additions & 0 deletions fp-course-haskell.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ extra-source-files:
library
exposed-modules:
Alternative
Anagrams
Applicative
Cheque
Comonad
Compose
Contravariant
Core
ExactlyOne
Extend
FastAnagrams
FileIO
Functor
Interactive
JsonParser
JsonValue
List
Expand Down Expand Up @@ -62,6 +66,7 @@ test-suite fp-course-test
other-modules:
AlternativeSpec
ApplicativeSpec
ChequeSpec
ComonadSpec
ComposeSpec
ContravariantSpec
Expand Down
38 changes: 38 additions & 0 deletions src/Anagrams.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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.<$>)
Loading

0 comments on commit c08fcd6

Please sign in to comment.