使这个功能默认

Make this function tacit

为了简化它,我想让这个函数没有点。我显式地传递长度而不是计算它,因为我还需要它用于其他功能,而且我只想计算一次。我设法摆脱了目标字符串参数,但在处理其他两个参数时遇到了困难。

-- 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))

我严重怀疑这个特定的函数是否可以通过以无点样式编写来变得更简单。例如,这是我从 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) . (.) . (!!)

遵循@Reid Barton 的建议:

-- 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 -> String -> String
cycleWS = align . cycle

-- Align with respect to spaces.
-- Given two strings, source and target, align the source to target such that:
-- 1. both will have the same length
-- 2. spaces in the target string will remain spaces
-- Assume the source string is long enough 
align :: String -> String -> String
align [] _  = []
align _ [] = []
align (x:xs) (y:ys) = if isSpace y then y : align (x:xs) ys else x : align xs ys