ftw
ftw

Reputation: 385

Haskell pattern matching on List

I know we can use below pattern matching in Haskell:

sum :: (Num a) => [a] -> a  
sum [] = 0  
sum (x:xs) = x + sum xs 

But why can’t we use [x] ++ xs?

sum :: (Num a) => [a] -> a  
sum [] = 0  
sum ([x] ++ xs) = x + sum xs

Upvotes: 3

Views: 704

Answers (1)

Thomas M. DuBuisson
Thomas M. DuBuisson

Reputation: 64740

You can pattern match using constructors and literals but not functions.

Upvotes: 5

Related Questions