Search code examples
elm

List split in Elm


Write a function to split a list into two lists. The length of the first part is specified by the caller.

I am new to Elm so I am not sure if my reasoning is correct. I think that I need to transform the input list in an array so I am able to slice it by the provided input number. I am struggling a bit with the syntax as well. Here is my code so far:

listSplit: List a -> Int -> List(List a)
listSplit inputList nr = 
let myArray = Array.fromList inputList
    in Array.slice 0 nr myArray 

So I am thinking to return a list containing 2 lists(first one of the specified length), but I am stuck in the syntax. How can I fix this?


Solution

  • Alternative implementation:

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