Problem 18 Solutions

Solution 1

sublist : Int -> Int -> List a -> List a 
sublist start end list = 
    let
        s_ = max (start-1) 0
        e_ = max end 0
    in 
        List.drop s_ list |> List.take (e_ - s_)

Solution 2

Use Array.slice. Note that Array.slice uses negative indices to count from the end of the array.

sublist : Int -> Int -> List a -> List a 
sublist start end list = 
    let 
        s' = max (start-1) 0
        e' = max end 0
    in 
        Array.fromList list
            |> Array.slice s' e'
            |> Array.toList

Back to problem

results matching ""

    No results matching ""