F#是否与Haskell相当? [英] Does F# have an equivalent to Haskell's take?

查看:115
本文介绍了F#是否与Haskell相当?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Haskell中,有一个函数take n list,它返回列表中的前n个元素。例如,sum(take 3 xs)总结列表xs中的前三个元素。 F#有相同的吗?我认为它是列表功能之一,但我无法找到任何似乎匹配的东西。

解决方案

是的,它叫做 Seq.take 。用法看起来与Haskell相同: Seq.take count source 要在列表中使用它,请使用 List.toSeq (更新:显然来自评论,这是没有必要的。)


In Haskell, there is a function "take n list" which returns the first n elements from a list. For example "sum (take 3 xs)" sums up the first three elements in the list xs. Does F# have an equivalent? I expected it to be one of the List-functions, but I can't spot anything that seems to match.

解决方案

Yeah, it's called Seq.take. Usage seems to be identical to Haskell's: Seq.take count source. To use it on a list, use List.toSeq first. (Update: Apparently from the comments, this isn't necessary.)

这篇关于F#是否与Haskell相当?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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