Haskell list comprehension
sum1::[Int]->[Int]
sum1 [] =0
sum1 (x:xs) = [x开发者_JS百科|x<-xs,x `mod` 2 ==0]
I need to return the numbers which are divisible by 2 when a list is provided, but the above code gives me this compile error:
Instance of Num [Int] required for definition of sum1
Also please explain what (x:xs) does. Is it 'X element of xs list?'
If we want to get a nth element of the list, how do we get it?
You've asked a few questions so I will answer them one by one.
Question 1: why does the compiler tell me Instance of Num [Int] required for definition of sum1?
You've created a function called sum1 with the type [Int]->[Int]. However, consider the line sum1 [] = 0: this returns Int, not [Int]. The solution to this problem is to change the line to sum1 [] = [].
Question 2: what does (x:xs) mean?
Haskell allows you to perform something called pattern matching. Without going into too much detail, as there are plenty of better explanations, the effect is that x is the first element of the list and xs is the rest of the list -- that is, you've peeled off the first element of the list.
For example, if you called sum1 [1,2,3], x would be 1 and xs would be [2,3].
Question 3: if we want to get a nth element of the list how to get it?
A common way to do this is to use the !! function -- which is infixed, that is, you provide both a left and right argument just as you would for + or *. For example, [1,2,3]!!1 will return 2.
Now, if this has helped clarify things, you should notice that your function definition has some problems. I am not sure if you wanted to work these out yourself or not.
sum1::[Int]->[Int]
sum1 [] = [0] // problem here
sum1 (x:xs) = [x| x <- xs ,x `mod` 2 == 0]
sum1 must return [Int] but in this case it is returning only Int.
Pattern matching a list with (x:xs) will put the first element of the list in x and the rest of the list in xs.
If you want sum1 to return all the even numbers from the list provided you should change your code to
sum1::[Int]->[Int]
sum1 [] = [0] // problem here
sum1 xs = [x| x <- xs ,x `mod` 2 == 0]
Because, your current code will skip first element of the list. Try with [2..10] as input. You will not see 2 in the output list.
To get nth element from the list xs haskell syntax is
xs !! n
Example
*Main> [1..10] !! 5
6
Next time you get into trouble see Some Common (and not so common!) Hugs Errors.
Well, they answered you correctly, but anyways I'm going to tell you something they didn't :P
Question 2: what does (x:xs) mean?
The lists in Haskell are made by: HEAD + TAIL, where HEAD is the first element and TAIL are the others.
"head [1,2,3]" will return 1
"tail [1,2,3]" will return [2,3]
But there are another functions anyways:
"init [1,2,3] will return [1,2] "last [1,2,3] will return 3
If you are starting at Haskell, check this
PS: Sorry for my bad English!
You don't need an anker using list comprehension!
sum1 = \xs -> [ x | x <- xs, mod x 2 == 0]
Another solution for the nth-element question:
nth_elem = \n xs -> head $ [ x | (id,x) <- zip [1..] xs, id == n ]
加载中,请稍侯......
精彩评论