在Haskell中获取一个子列表 [英] Get a sublist in Haskell

查看:95
本文介绍了在Haskell中获取一个子列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能很简单,但我浏览过文档并以Google搜索为例,我仍然不确定答案。



如果我有像这样的列表:

  [1,2,3,4,5,6,7,8,9,0] 

我想提取一个切片,从索引4到索引8,即我想要的:

  [5,6,7,8,9] 

在Haskell中做到这一点的惯用方法是什么?

首先,这不是一个数组,它是一个列表。我不是(仅)迂腐,因为数组在Haskell中的问题比列表中多得多。



也就是说,一种常见的方法是使用 drop 在一起:

 前奏>下降4。取9美元[1,2,3,4,5,6,7,8,9,0] 
[5,6,7,8,9]
前奏>拿(9-4)。 drop 4 $ [1,2,3,4,5,6,7,8,9,0]
[5,6,7,8,9]

后者效率更高一些。


Probably an easy one, but I've looked through the docs and googled for examples and I'm still not sure of the answer.

If I have a list like this:

[1,2,3,4,5,6,7,8,9,0]

and I want to extract a slice, say from index 4 to index 8 i.e. I want:

[5,6,7,8,9]

What is the idiomatic way to do this in Haskell?

解决方案

First of all, that's not an array, it's a list. I'm not being (merely) pedantic, as arrays are much more problematic in Haskell than lists.

That said, one common way is to use take and drop together:

Prelude> drop 4 . take 9 $ [1,2,3,4,5,6,7,8,9,0]
[5,6,7,8,9]
Prelude> take (9-4) . drop 4 $ [1,2,3,4,5,6,7,8,9,0]
[5,6,7,8,9]

The latter is a bit more efficient.

这篇关于在Haskell中获取一个子列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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