小弟有几个Haskell问题想问大神

1个回答

  • 第一道用了一个比较笨的办法,而且也没有管列表长度小于2的情况

    ```haskell

    splits xs = sp ((length xs)-1) xs

    sp 1 xs = [splitAt 1 xs]

    sp n xs = (sp (n-1) xs) ++ [splitAt n xs]

    ```

    第二道题copy自[stackoverflow](http://stackoverflow.com/a/21722065/2214113)

    ```haskell

    commonPrefix :: (Eq e) => [e] -> [e] -> [e]

    commonPrefix _ [] = []

    commonPrefix [] _ = []

    commonPrefix (x:xs) (y:ys)

    | x == y = x : commonPrefix xs ys

    | otherwise = []

    cpfx :: (Eq a) => [[a]] -> [a]

    cpfx = foldl1 commonPrefix

    ```

    该睡了,后面两个改天有时间再说,我才不告诉你我不会.