Haskell从String中删除Chars [英] Haskell delete Chars from String

查看:205
本文介绍了Haskell从String中删除Chars的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个形式的函数

f :: String -> [String]
f str = ...

返回所有形成的字符串通过从 str 中删除​​一个字符。例如:

that returns the list of all the strings formed by removing exactly one character from str. For example:

ghci> f "stack"
["tack","sack","stck","stak","stac"]

$ b b

因为 String [Char] 是同义词,我可以使用索引,我知道你应该避免在Haskell这样做。除了使用索引,还有更好的方法吗?

Because String and [Char] are synonymous, I could use the index, but I know that you should avoid doing that in Haskell. Is there a better way besides using the index?

推荐答案

你可以使用递归:

f :: [a] -> [[a]]
f [] = []
f (s:ss) = ss : map (s:) (f ss)

这篇关于Haskell从String中删除Chars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆