| Copyright | © Frank Jung 2017-2026 |
|---|---|
| License | BSD-3-Clause |
| Maintainer | frankhjung@linux.com |
| Stability | stable |
| Portability | portable |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
WordPuzzle
Description
Supporting functions for solving letter word puzzles.
Notes
Is functions return a boolean:
isXyz :: a -> Bool
Validate functions return a Validation:
validateXyz :: a -> Validation [ValidationError] a
Synopsis
- data WordPuzzle = WordPuzzle {}
- validateSize :: Int -> Validation [ValidationError] Int
- validateLetters :: String -> Validation [ValidationError] String
- nineLetters :: String -> ByteString -> Bool
- spellingBee :: String -> ByteString -> Bool
- solve :: WordPuzzle -> IO ()
- solver :: WordPuzzle -> InputStream ByteString -> IO (InputStream ByteString)
- data ValidationError
- validate :: Bool -> Int -> String -> FilePath -> Validation [ValidationError] WordPuzzle
- toEither :: Validation e a -> Either e a
Documentation
data WordPuzzle Source #
Represent parameters required for the puzzle.
Constructors
| WordPuzzle | |
Instances
| Show WordPuzzle Source # | |
Defined in WordPuzzle Methods showsPrec :: Int -> WordPuzzle -> ShowS # show :: WordPuzzle -> String # showList :: [WordPuzzle] -> ShowS # | |
validateSize :: Int -> Validation [ValidationError] Int Source #
Validate size of word.
validateLetters :: String -> Validation [ValidationError] String Source #
Validate letters.
Arguments
| :: String | valid letters |
| -> ByteString | dictionary word to check |
| -> Bool | true if dictionary word matches letters |
Check if a word contains only characters from a letters list.
Uses a strict left fold over the input word with accumulator (mask, ok):
maskis a bitset of already seen letters.oktracks whether all checks have passed so far.
For each character, the fold:
- fails if the character is not in the valid letter pool,
- fails if the character bit is already set (repeated letter),
- otherwise sets the bit and continues.
Assumes that the letter pool is valid (see isLetters).
Arguments
| :: String | valid letters |
| -> ByteString | dictionary word to check |
| -> Bool | true if dictionary word matches letters |
Check if a word contains only characters from a letters list. Repeating characters are allowed.
Assumes that the letter pool is valid (see isLetters).
solve :: WordPuzzle -> IO () Source #
Solve word puzzle given a dictionary of words.
Where each word:
- must be greater than the minimum word length
- must contain mandatory character
- must contain only valid characters
- if repeats are not allowed, must not exceed valid character frequency
- when repeats are enabled there is no upper limit on word length; the word may be longer than the letter pool itself.
Example:
solve (WordPuzzle 4 a "abcdefghij" "dictionary.txt")
Method:
- open file
- split into lines
- filter
- print each
- force traversal to EOF
solver :: WordPuzzle -> InputStream ByteString -> IO (InputStream ByteString) Source #
Filter words from an input stream based on the puzzle constraints.
data ValidationError Source #
Error given on invalid parameter.
Constructors
| InvalidSize (Int, Int) Int | expected range and actual size |
| InvalidLetters String | actual letters (should be 4-9 unique lowercase letters) |
| UnexpectedValue String | couldn't parse value |
Instances
| Show ValidationError Source # | Show |
Defined in WordPuzzle Methods showsPrec :: Int -> ValidationError -> ShowS # show :: ValidationError -> String # showList :: [ValidationError] -> ShowS # | |
| Eq ValidationError Source # | |
Defined in WordPuzzle Methods (==) :: ValidationError -> ValidationError -> Bool # (/=) :: ValidationError -> ValidationError -> Bool # | |
validate :: Bool -> Int -> String -> FilePath -> Validation [ValidationError] WordPuzzle Source #
Validate program parameters.
toEither :: Validation e a -> Either e a #
Converts from Validation to Either.