deepthroat
Erfahrenes Mitglied
Hi.
Mein Lösungsvorschlag in Haskell:
Gruß
PS: Schade das es kein Code Tag speziell für Haskell oder Prolog gibt...
Mein Lösungsvorschlag in Haskell:
Code:
module Main where
import System.Environment (getArgs)
presidents = [ "George Washington", "John Adams", "Thomas Jefferson", "James Madison", "James Monroe",
"John Quincy Adams", "Andrew Jackson", "Martin Van Buren", "William Harrison",
"John Tyler", "James Polk", "Zachary Taylor", "Millard Fillmore", "Franklin Pierce",
"James Buchanan", "Abraham Lincoln", "Andrew Johnson", "Ulysses Grant", "Rutherford Hayes",
"James Garfield", "Chester Arthur", "Grover Cleveland", "Benjamin Harrison",
"Grover Cleveland", "William McKinley", "Theodore Roosevelt", "William Taft", "Woodrow Wilson",
"Warren Harding", "Calvin Coolidge", "Herbert Hoover", "Franklin Roosevelt", "Harry Truman",
"Dwight Eisenhower", "John F. Kennedy", "Lyndon Johnson", "Richard Nixon", "Gerald Ford",
"Jimmy Carter", "Ronald Reagan", "George H. Bush", "Bill Clinton", "George W. Bush" ]
sep = [ "<", "", ">" ]
fuzzy_search :: String -> String -> [String]
fuzzy_search needle haystack =
fz_search (filter (' ' /=) needle) haystack sep "" where
fz_search (n:p) "" _ _ = [ ]
fz_search "" h s acc = [ reverse ((s !! 1) ++ acc) ++ h ]
fz_search (n:ns) (h:hs) s acc
| n == h = fz_search ns hs (tail sep) (h: s !! 0 ++ acc)
| otherwise = fz_search (n:ns) hs sep (h: s !! 1 ++ acc)
find_all :: String -> [String]
find_all s = concat $ map ( fuzzy_search s ) presidents
-- Main
main = do args <- getArgs
case args of
[] -> putStrLn "Usage: quiz1 SEARCH..."
it -> putStr $ unlines $ concat $ map find_all it
PS: Schade das es kein Code Tag speziell für Haskell oder Prolog gibt...