在F#中对列表进行分块时我做错了什么 [英] What am I doing wrong in chunking a list in F#

查看:52
本文介绍了在F#中对列表进行分块时我做错了什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

F#对我来说并不容易.以下代码段应该对列表进行分块.我不知道是什么问题.请帮忙.

F# doesn't come easy to me. The following piece of code is supposed to chunk a list. I have no idea what the problem is. Please help.

let chunk items chunkSize =
    let folder = fun state x ->
        match state with (reversedResult, reversedChunk) ->
            if reversedChunk.Length < chunkSize then
                (reversedResult, x::reversedChunk)
            else
                ((reversedChunk |> List.rev)::reversedResult, [x])
    let alsmostDone = items |> List.fold folder ([], [])
    match alsmostDone with
    | (reversedResult, []) -> reversedResult |> List.rev
    | (reversedResult, lastReversedChunk) -> (lastReversedChunk |> List.rev)::reversedResult |> List.rev

推荐答案

我认为使用List.length有点惯用" f#.然后,您不需要任何类型注释.所以:

I think using List.length is a bit more "idiomatic" f#. And then you don't need any type annotations. So:

...
if List.length reversedChunk < chunkSize then
...

这篇关于在F#中对列表进行分块时我做错了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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