错误:表达式中的语法错误(意外的`|')

时间:2015-03-30 08:10:30

标签: haskell

Ι想在列表中实现这种结构

Main> update 1 []
[1]
Main> update 2 [2]
[2]
Main> update 6 [1,2,5,8]
[1,2,5,8,6]
Main> 7 [7,7,7,3,4,5,8]
[3,4,5,8,7]
-}

我的代码是,

update :: Int->[Int]->[Int]

update n s  
    = update1 n s


update1 :: Int->[Int]->[Int] 
update1 n (h:t)
    | h==[] =  n: 
    | n==h = update1 n t 
    | otherwise = h : update1 n t

为什么 我错了?

1 个答案:

答案 0 :(得分:4)

请注意n:不正确。您要做的是返回[n]n:[]。您还可以使用模式匹配来检查空案例:

update1 :: Int->[Int]->[Int] 
update1 n [] = [n]
update1 n (h:t)
    | n == h = update1 n t 
    | otherwise = h : update1 n t
相关问题