Skip to content

Instantly share code, notes, and snippets.

@ardwalker
Created September 23, 2019 08:57
Show Gist options
  • Save ardwalker/5b90bde16bac430e711d5c12c6116905 to your computer and use it in GitHub Desktop.
Save ardwalker/5b90bde16bac430e711d5c12c6116905 to your computer and use it in GitHub Desktop.
Simple speller
import Debug.Trace
-- RULES
-- speller ["abacus"] -- > "a is for abacus"
-- speller [] -- > ""
-- speller ["apple", "banana", "coconut"]
-- -- > "a is for apple, b is for banana, and c is for coconut"
-- speller ["whisky", "x-ray"]
-- -- > "w is for whisky, and x is for x-ray"
-- Creates the phrase for each word in the list entered
phrase :: [Char] -> [Char]
phrase word = [(word !! 0)] ++ " is for " ++ word
-- A bit ugly, is there a better way?
-- This ensures that the position of [, and] are correct
position :: [Char] -> [Char] -> [Char]
position el acc =
if (acc /= "") then (phrase el) ++ ", " ++ acc
else "and " ++ (phrase el)
-- Takes the array of strings entered and accumulates all the phrases
-- Matches
-- 1. Empty list
-- 2. Single phrase, no 'and' required
-- 3. Every other case
display :: [[Char]] -> [Char]
display [] = ""
display (w:[]) = phrase w
display (w) = foldr (\el acc -> position el acc) "" w
-- Entry point
speller :: [[Char]] -> [Char]
speller words = display words
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment