Matthias Reitinger
ɐɯıǝɹ
Einfache Variante:
Mit der Erweiterung:
Mit Kommentaren habe ich etwas gespart. Falls jemand tatsächlich Interesse an einer Erklärung hat, lasst es mich wissen
Code:
import System (getArgs)
import Data.Char (isSpace)
fuzzyMatch :: String -> String -> Bool
fuzzyMatch "" _ = True
fuzzyMatch _ "" = False
fuzzyMatch qry@(q:qs) str@(s:ss)
| isSpace q = fuzzyMatch qs str
| q == s = fuzzyMatch qs ss
| otherwise = fuzzyMatch qry ss
main = do
cnts <- readFile "presidents.txt"
args <- getArgs
if (null args) then putStrLn "missing argument"
else do
let results = filter (fuzzyMatch (head args)) (lines cnts)
putStr (unlines results)
Mit der Erweiterung:
Code:
import System (getArgs)
import Data.Char (isSpace)
import Data.Maybe (catMaybes)
mapMaybe :: (a -> b) -> Maybe a -> Maybe b
mapMaybe f Nothing = Nothing
mapMaybe f (Just a) = Just (f a)
consIf :: Bool -> a -> [a] -> [a]
consIf False _ list = list
consIf True a list = a:list
fuzzyMatch :: String -> String -> Maybe String
fuzzyMatch = fuzzyMatch' False
fuzzyMatch' :: Bool -> String -> String -> Maybe String
fuzzyMatch' parenOpen "" str = Just (consIf parenOpen '>' str)
fuzzyMatch' _ _ "" = Nothing
fuzzyMatch' parenOpen qry@(q:qs) str@(s:ss)
| isSpace q = fuzzyMatch' parenOpen qs str
| q == s = mapMaybe (consIf (not parenOpen) '<' . (:) s)
(fuzzyMatch' True qs ss)
| otherwise = mapMaybe (consIf parenOpen '>' . (:) s)
(fuzzyMatch' False qry ss)
main = do
cnts <- readFile "presidents.txt"
args <- getArgs
if (null args) then putStrLn "missing argument"
else do
let results = catMaybes (map (fuzzyMatch (head args)) (lines cnts))
putStr (unlines results)
Mit Kommentaren habe ich etwas gespart. Falls jemand tatsächlich Interesse an einer Erklärung hat, lasst es mich wissen
