Haskell 使这个函数默认

Haskell 使这个函数默认,haskell,pointfree,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 th

为了简化它,我想把这个函数设为自由点。我显式地传递长度而不是计算它,因为其他函数也需要它,我只想计算一次。我已经设法去掉了目标字符串参数,但仍在努力处理另外两个参数

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

我非常怀疑,用无点的方式编写这个函数是否可以使它变得更简单。例如,以下是我从中得到的信息:


我非常怀疑,用无点的方式编写这个函数是否可以使它变得更简单。例如,以下是我从中得到的信息:


按照@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

按照@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


不如把它分成易于理解的几部分来简化它吧?当然,我愿意接受建议。好吧,让一个函数将一个字符串和另一个字符串中的空格对齐,再加上
cycle
@ReidBarton,谢谢,实现了。如果
length str!=len
?没有理由将长度作为显式参数传递;只要从给定的字符串中推断出来就可以了。不如把它分解成易于理解的片段来简化它吧?当然,我愿意接受建议。那么,用一个函数将一个字符串与另一个字符串中的空格对齐,再加上
cycle
@ReidBarton,谢谢,实现了。如果
length str!=len
?没有理由将长度作为显式参数传递;从给定的字符串中推断出来。我想知道
flip-flip
是否可以取消,因为它们取消了吗?@dimid不,它们没有取消。请记住,函数应用程序是左关联的,因此例如,
flip-flip-snd
(flip-flip)snd
相同,而不是
flip(flip-snd)
。我想知道
flip-flip
是否可以取消,因为它们取消了?@dimid不,它们没有取消。请记住,函数应用程序是左关联的,因此,例如,
flip-snd
(flip-flip)snd
相同,而不是
flip(flip-snd)
。您可以使用
isSpace
获取更多空格字符。顺便说一句,这远远优于您最初使用的代码。我对
过敏。出于好奇,为什么?是
被认为是一种代码气味?这是一种部分功能,而且速度也很慢。组合太糟糕了。你可以使用
isSpace
来获得更多的空格字符。顺便说一句,这远远优于你最初使用的代码。我对
过敏。出于好奇,为什么?是
被认为是一种代码气味?这是一种部分功能,而且速度也很慢。这种组合很糟糕。