Simple haskell splitlist

后端 未结 2 1016
闹比i
闹比i 2020-12-21 03:25

I have the following function which takes a list and returns two sublists split at a given element n. However, I only need to split it in half, with odd length lists having

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-21 03:26

    You can do it using take and drop:

    splitlist :: [a] -> ([a],[a])
    splitlist [] = ([],[])
    splitlist l  = let half = (length(l) +1)`div` 2
                   in (take half l, drop half l)
    

    or you can take advantage of the function splitAt:

    splitlist list = splitAt ((length (list) + 1) `div` 2) list
    

提交回复
热议问题