heiLou
heiLou

Reputation: 207

ERROR : Syntax error in expression (unexpected `|')

Ι want to achieve this structure in lists

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]
-}

and my code is ,

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

Where and why am i wrong ?

Upvotes: 2

Views: 776

Answers (1)

Sibi
Sibi

Reputation: 48756

Note that n: is not correct. What you want to do is instead return [n] or n:[]. Also you can use pattern matching to check for the empty case:

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

Upvotes: 4

Related Questions