0

如何在 Haskell 中删除列表的一部分?这是我到目前为止所做的。请告诉我可以进行哪些更改:

import Data.List
import Data.List.Split

removePrefix :: Eq t => [t] -> [t] -> Maybe [t]
removePrefix [] [] = Nothing
removePrefix ts [] = Just ts 
removePrefix (t:ts) (y:ys) = 
  if inTnFixOf (y:ys) (t:ts) == True
  then SrtipPrefix (y:ys) (t:ts)
  else Just [(x:xs)]

示例:输入“toyboat”输出“boat”

4

2 回答 2

2

已经存在的功能stripPrefix来自Data.List

 stripPrefix "foo" "foobar" == Just "bar"
 stripPrefix "foo" "foo" == Just ""
 stripPrefix "foo" "barfoo" == Nothing
 stripPrefix "foo" "barfoobaz" == Nothing

定义为

stripPrefix :: Eq a => [a] -> [a] -> Maybe [a]
stripPrefix [] ys = Just ys
stripPrefix (x:xs) (y:ys)
 | x == y = stripPrefix xs ys
stripPrefix _ _ = Nothing

所以,你可以声明:

removePrefix [] [] = Nothing
removePrefix xs ys = stripPrefix xs ys
于 2013-09-01T17:28:57.847 回答
1

以下内容从给定列表中删除所有子列表:

import Data.List.Split

rmSublist :: Eq a => [a] -> [a] -> [a]
rmSublist [] _ = []
rmSublist _ [] = []
rmSublist xs ys = concat $ filter (/=[]) (splitOn xs ys)

或者,如果您只想删除前缀:

rmPrefix :: Eq a => [a] -> [a] -> [a]
rmPrefix [] ys = ys
rmPrefix _ [] = []
rmPrefix xs ys =
  if xs == take n ys
  then drop n ys
  else ys
  where n = length xs
于 2013-09-01T01:06:52.273 回答