gpt4 book ai didi

elm - Elm 中的列表拆分

转载 作者:行者123 更新时间:2023-12-03 22:54:36 28 4
gpt4 key购买 nike

编写一个函数将一个列表分成两个列表。第一部分的长度由调用者指定。
我是 Elm 的新手,所以我不确定我的推理是否正确。我认为我需要转换数组中的输入列表,以便我能够按提供的输入数字对其进行切片。我也在语法上有点挣扎。到目前为止,这是我的代码:

listSplit: List a -> Int -> List(List a)
listSplit inputList nr =
let myArray = Array.fromList inputList
in Array.slice 0 nr myArray
所以我想返回一个包含 2 个列表(指定长度的第一个)的列表,但我被困在语法中。我怎样才能解决这个问题?

最佳答案

替代实现:

split : Int -> List a -> (List a, List a)
split i xs =
(List.take i xs, List.drop i xs)

关于elm - Elm 中的列表拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66438875/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com