Word break problem: Difference between revisions

m
m (Minor edit to C++ code)
Line 225:
=={{header|Haskell}}==
{{Trans|Javascript}}
<lang haskell>import Data.TreeList (isPrefixOf, intercalate)
import Data.ListTree (isPrefixOf, intercalateTree(..))
 
wordBreaks :: [String] -> String -> String
wordBreaks ws = (++) <*> \s =-> ":\n" ++ report (go <$> tokenTrees ws s)
where
let parses = go <$> tokenTrees ws s
go t
| null (subForest t) = [rootLabel t]
| otherwise = subForest t >>= ((:) (rootLabel t) . go)
report xs
| null xs = "\tNot parseable with these words"
| otherwise = unlines $ (('\t' :) . intercalate " -> ") <$> xs
in s ++ (':' : '\n' : report parses)
 
tokenTrees :: [String] -> String -> [Tree String]
Line 252 ⟶ 251:
| otherwise = [Node w xs]
 
-- TEST --------------------------------- TEST ---------------------------
 
-- TEST ------------------------------------------------------------
ws, texts :: [String]
ws = words "a bc abc cd b"
9,659

edits