如何在haskell中编写一个函数,它在String的每个字符后插入空格

时间:2017-11-14 06:25:37

标签: haskell

我想在haskeel中实现一个方法,它应该在传递给该方法的每个字符串之后添加空格,但不能在字符串的最后一个字符串之后添加空格  例如

Main> insertSpace "This is world"
"T h i s  i s  w o r l d"

1 个答案:

答案 0 :(得分:5)

您可以通过显式递归手动编写此内容。

insertSpace :: String -> String
insertSpace []     = []
insertSpace (x:[]) = x  -- you need this to keep from adding a space at the end
insertSpace (x:xs) = x:' ':(insertSpace xs)

Data.List - intersperse中有一个stdlib函数。

import Data.List (intersperse)

insertSpace :: String -> String
insertSpace = intersperse ' '

这是您在Char -> String -> String搜索apply plugin: 'findbugs' task findbugs(type: FindBugs) { ignoreFailures = false reportLevel = "medium" classes = files("${project.rootDir}/app/build/intermediates/classes") source = fileTree('src/main/java/') classpath = files() effort = "max" } 时的第一个结果。

相关问题