elm/exercises/isogram/Isogram.example.elm
2017-10-10 21:10:32 -04:00

76 lines
1.5 KiB
Elm

module Isogram exposing (isIsogram)
import String
import List
import Char
isLetter : Char -> Bool
isLetter c =
Char.isHexDigit c && (not <| Char.isDigit c)
isIsogram : String -> Bool
isIsogram sentence =
let
sanitized =
String.filter isLetter sentence
|> String.toLower
|> String.toList
|> List.sort
|> group
in
List.all (\x -> List.length x == 1) sanitized
-- Adapted from https://github.com/elm-community/list-extra
group : List a -> List (List a)
group list =
case list of
[] ->
[]
x :: xs ->
let
( ys, zs ) =
span ((==) x) xs
in
(x :: ys) :: group zs
span : (a -> Bool) -> List a -> ( List a, List a )
span p xs =
( takeWhile p xs, dropWhile p xs )
takeWhile : (a -> Bool) -> List a -> List a
takeWhile predicate =
let
takeWhileHelper acc list =
case list of
[] ->
List.reverse acc
x :: xs ->
if (predicate x) then
takeWhileHelper (x :: acc) xs
else
List.reverse acc
in
takeWhileHelper []
dropWhile : (a -> Bool) -> List a -> List a
dropWhile predicate list =
case list of
[] ->
[]
x :: xs ->
if (predicate x) then
dropWhile predicate xs
else
list