haskell: pangram

This commit is contained in:
James Walker 2021-09-11 12:53:48 -04:00
parent 8d7392977c
commit b1e69d7da9
Signed by: walkah
GPG Key ID: 3C127179D6086E93
9 changed files with 278 additions and 0 deletions

88
haskell/pangram/HELP.md Normal file
View File

@ -0,0 +1,88 @@
# Help
## Running the tests
To run the test suite, execute the following command:
```bash
stack test
```
#### If you get an error message like this...
```
No .cabal file found in directory
```
or
```
RedownloadInvalidResponse Request {
...
}
"/home/username/.stack/build-plan/lts-xx.yy.yaml" (Response {responseStatus = Status {statusCode = 404, statusMessage = "Not Found"},
```
You are probably running an old stack version and need
to upgrade it. Try running:
```bash
stack upgrade
```
Or see other options for upgrading at [Stack documentation](https://docs.haskellstack.org/en/stable/install_and_upgrade/#upgrade).
#### Otherwise, if you get an error message like this...
```
No compiler found, expected minor version match with...
Try running "stack setup" to install the correct GHC...
```
Just do as it says and it will download and install
the correct compiler version:
```bash
stack setup
```
If you want to play with your solution in GHCi, just run the command:
```bash
stack ghci
```
## Submitting your solution
You can submit your solution using the `exercism submit src/Pangram.hs` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Haskell track's documentation](https://exercism.org/docs/tracks/haskell)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
## Getting Started
Please refer to the [installation](https://exercism.io/tracks/haskell/installation)
and [learning](https://exercism.io/tracks/haskell/learning) help pages.
## Feedback, Issues, Pull Requests
The [exercism/haskell](https://github.com/exercism/haskell) repository on
GitHub is the home for all of the Haskell exercises.
If you have feedback about an exercise, or want to help implementing a new
one, head over there and create an issue. We'll do our best to help you!
{{ with .Spec.Credits }}

37
haskell/pangram/README.md Normal file
View File

@ -0,0 +1,37 @@
# Pangram
Welcome to Pangram on Exercism's Haskell Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Determine if a sentence is a pangram. A pangram (Greek: παν γράμμα, pan gramma,
"every letter") is a sentence using every letter of the alphabet at least once.
The best known English pangram is:
> The quick brown fox jumps over the lazy dog.
The alphabet used consists of ASCII letters `a` to `z`, inclusive, and is case
insensitive. Input will not contain non-ASCII symbols.
## Source
### Created by
- @lpalma
### Contributed to by
- @chiroptical
- @iHiD
- @navossoc
- @petertseng
- @ppartarr
- @rbasso
- @rsslldnphy
- @Sir4ur0n
- @sshine
- @tejasbubane
### Based on
Wikipedia - https://en.wikipedia.org/wiki/Pangram

View File

@ -0,0 +1,16 @@
name: pangram
dependencies:
- base
library:
exposed-modules: Pangram
source-dirs: src
tests:
test:
main: Tests.hs
source-dirs: test
dependencies:
- pangram
- hspec

View File

@ -0,0 +1,17 @@
name: pangram
dependencies:
- base
- text
library:
exposed-modules: Pangram
source-dirs: src
tests:
test:
main: Tests.hs
source-dirs: test
dependencies:
- pangram
- hspec

View File

@ -0,0 +1,8 @@
module Pangram (isPangram) where
import Data.Text (Text)
import qualified Data.Text as T
isPangram :: Text -> Bool
isPangram s = all (`member` T.toLower s) ['a'..'z']
where member = T.any . (==)

View File

@ -0,0 +1,21 @@
name: pangram
version: 2.0.0.12
dependencies:
- base
library:
exposed-modules: Pangram
source-dirs: src
ghc-options: -Wall
# dependencies:
# - foo # List here the packages you
# - bar # want to use in your solution.
tests:
test:
main: Tests.hs
source-dirs: test
dependencies:
- pangram
- hspec

View File

@ -0,0 +1,7 @@
module Pangram (isPangram) where
import Data.Char
isPangram :: String -> Bool
isPangram text = all hasLetter ['a' .. 'z']
where hasLetter l = any (== l) (map toLower text)

View File

@ -0,0 +1 @@
resolver: lts-16.21

View File

@ -0,0 +1,83 @@
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
import Data.Foldable (for_)
import Data.String (fromString)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)
import Pangram (isPangram)
main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs
specs :: Spec
specs = describe "isPangram" $ for_ cases test
where
test Case{..} = it description $ isPangram (fromString input) `shouldBe` expected
data Case = Case { description :: String
, input :: String
, expected :: Bool
}
cases :: [Case]
cases = [ Case { description = "with empty sentence"
, input = ""
, expected = False
}
, Case { description = "with perfect lower case"
, input = "abcdefghijklmnopqrstuvwxyz"
, expected = True
}
, Case { description = "with only lower case"
, input = "the quick brown fox jumps over the lazy dog"
, expected = True
}
, Case { description = "with missing character 'x'"
, input = "a quick movement of the enemy will jeopardize five gunboats"
, expected = False
}
, Case { description = "with missing character 'h'"
, input = "five boxing wizards jump quickly at it"
, expected = False
}
, Case { description = "with underscores"
, input = "the_quick_brown_fox_jumps_over_the_lazy_dog"
, expected = True
}
, Case { description = "with numbers"
, input = "the 1 quick brown fox jumps over the 2 lazy dogs"
, expected = True
}
, Case { description = "with missing letters replaced by numbers"
, input = "7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog"
, expected = False
}
, Case { description = "with mixed case and punctuation"
, input = "\"Five quacking Zephyrs jolt my wax bed.\""
, expected = True
}
, Case { description = "with mixed case"
, input = "the quick brown fox jumps over with lazy FX"
, expected = False
}
, Case { description = "with missing character and non-ascii letters"
, input = "abcdefghijklmnopqrstuvwxyÃ"
, expected = False
}
, Case { description = "with additional non-ascii letters"
, input = "abcdefghijklmnopqrstuvwxyzÃ"
, expected = True
}
{-
-- The following test can be enabled for String-based solutions:
, Case { description = "with termination as soon as all letters have occurred"
, input = "abcdefghijklmnopqrstuvwxyz" ++ [undefined]
, expected = True
}
-- -}
]
-- 73e37723b154f0c741d227c43af09d23dc7e5e44