【发布时间】:2020-07-04 01:53:29
【问题描述】:
insert_at 将元素e插入到列表xs的特定位置n
testgetting Left 如果n 小于 0
test2 得到Left 如果n 大于xs 的长度或e 类型与列表xs 中的元素类型不匹配。否则将Right xs 传递给下一个。
import Data.Typeable
insert_at :: a -> [a] -> Int -> [a]
insert_at e xs n = a++(e:b) where
t = splitAt n xs
a = fst t
b = snd t
test :: (Ord a, Num a) => b -> a -> Either [Char] b
test xs n = if n < 0 then Left "n<0" else Right xs
test2 :: (Typeable a1, Typeable a2) =>
a1 -> [a2] -> Int -> Either [Char] [a2]
test2 e xs n
| n> ( length xs )= Left "n> $ length xs "
| (typeOf e) /= (typeOf (head xs) ) = Left "(typeOf e) /= (typeOf (head xs) ) "
|otherwise = Right xs
sf :: Typeable a => a -> [a] -> Int -> Either [Char] [a]
sf e xs n = test xs n >>test2 e xs n >> Right (insert_at e xs n)
所有其他错误都得到了正确处理,期待这个。
* No instance for (Num Char) arising from the literal `1'
* In the expression: 1
In the second argument of `sf', namely `[1, 2, 3, 4, ....]'
In the expression: sf 'a' [1, 2, 3, 4, ....] 3
【问题讨论】:
-
小澄清:你到底在问什么?您想知道为什么在帖子末尾出现错误,还是有其他问题?
-
是的,帖子最后的错误。