wordpuzzle-1.0.3: word puzzle solver
Copyright© Frank Jung 2017-2026
LicenseBSD-3-Clause
Maintainerfrankhjung@linux.com
Stabilitystable
Portabilityportable
Safe HaskellSafe-Inferred
LanguageHaskell2010

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

Documentation

data WordPuzzle Source #

Represent parameters required for the puzzle.

Constructors

WordPuzzle 

Fields

Instances

Instances details
Show WordPuzzle Source # 
Instance details

Defined in WordPuzzle

validateSize :: Int -> Validation [ValidationError] Int Source #

Validate size of word.

nineLetters Source #

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):

  • mask is a bitset of already seen letters.
  • ok tracks 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).

spellingBee Source #

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

Instances details
Show ValidationError Source #

Show ValidationError as string.

Instance details

Defined in WordPuzzle

Eq ValidationError Source # 
Instance details

Defined in WordPuzzle

validate :: Bool -> Int -> String -> FilePath -> Validation [ValidationError] WordPuzzle Source #

Validate program parameters.

toEither :: Validation e a -> Either e a #

Converts from Validation to Either.