-4 haskell
我正在尝试设计一个函数,当给定一个字符串时,该函数将a和b插入字符串中.
例如:
鉴于字符串 "Hello"
该函数将返回"aHbealblaob".
去搞清楚
Prelude> 'a' : (concat $ zipWith (\x y -> x:[y]) "Hello" $ cycle "ba")
"aHbealblaob"
Run Code Online (Sandbox Code Playgroud)
或者没有任何细节,这也会做.
f :: String -> String
f "Hello" = "aHbealblaob"
f _ = "not specified"
Run Code Online (Sandbox Code Playgroud)
实际上,有一个很好的相互递归解决方案,类似于奇数/偶数.
wrap [] = []
wrap (x:xs) = 'a':x:'b':skip xs
skip [] = []
skip (x:xs) = x: wrap xs
wrap "Hello"
"aHbealblaob"
Run Code Online (Sandbox Code Playgroud)