如何制作子串列表? [英] How do I make a list of substrings?

查看:72
本文介绍了如何制作子串列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图列出所有子字符串,其中每个子字符串只有一个少于原始字符串的元素。

例如1234会导致[1234,123,12,1]

我希望只使用前奏(无导入)来实现这一点,所以不能使用子序列。



我是Haskell的新手,我知道一些问题与我的代码,但目前不知道如何解决它们。

  slist :: String  - > [String] 
slist(x:xs)=(take(length(x:xs))(x:xs))++ slist xs

如何递归地使用

编辑:想要通过递归地使用init

解决方案

  slist :: String  - > [String] 
slist [] = []
- slist xs = [xs] ++(slist $ init xs)
slist xs = xs:(slist $ init xs)

main = do
print $ slist1234


I am trying to make a list of all substrings where each substring has one less element of the originial string.

e.g "1234" would result in ["1234","123","12","1"]

I would like to achieve this only using prelude (no import) so cant use subsequences.

I am new to Haskell, and I know some of the problems with my code but don't currently know how to fix them.

slist :: String -> [String]
slist (x:xs) = (take (length (x:xs)) (x:xs)) ++ slist xs

How can I do this recursively using

Edit: would like to this by using init recursively

解决方案

slist :: String -> [String]
slist [] = []
-- slist xs = [xs] ++ (slist $ init xs)
slist xs = xs : (slist $ init xs)

main = do 
    print $ slist "1234"

这篇关于如何制作子串列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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