使这个功能默认

时间:2016-09-04 03:26:08

标签: haskell pointfree

我想使这个功能无点,以简化它。我明确地传递了长度而不是计算它,因为我还需要它用于其他功能,并且我只想计算它一次。我已经成功摆脱了目标字符串参数,但与其他两个人争吵。

-- Cycle with respect to whitespace (currently only spaces).
-- Given a source string and its length, and a longer target string
-- (which may contain spaces) match the source to target such that:
-- 1. both will have the same length
-- 2. spaces in the target string will remain spaces
-- 3. chars from the source string will be cycled
--
-- Example:
-- src: "ALLY", len: 4
-- target: "MEET AT DAWN"
-- Result: "ALLY AL LYAL"                  
cycleWS :: String -> Int -> String -> String
cycleWS str len = fst . (foldr (\x (s, n) -> if x == ' ' then (s ++ " ", n) else (s ++ [str !! (n `mod` len)], n + 1)) ("", 0))

2 个答案:

答案 0 :(得分:7)

关注@Reid Barton的建议:

subDzs

答案 1 :(得分:5)

我严重怀疑通过以无点样式编写这个特定函数可以变得更简单。例如,这是我从pointfree.io得到的:

cycleWS = ((fst .) .) . flip flip (([]), 0) . (foldr .) . flip flip snd . ((flip . (ap .)) .) . flip flip fst . ((flip . ((.) .) . flip (ap . (ap .) . (. ((,) . (++ " "))) . (.) . if' . (' ' ==))) .) . flip flip (1 +) . ((flip . (liftM2 (,) .) . flip ((.) . (++))) .) . flip flip ([]) . ((flip . ((:) .)) .) . (. flip mod) . (.) . (!!)