Singleton list confusion
From HaskellWiki
(Difference between revisions)
(short introduction) |
(example) |
||
| (3 intermediate revisions not shown.) | |||
| Line 1: | Line 1: | ||
Why do Haskell newcomers frequently believe, that list variables must be enclosed in brackets? | Why do Haskell newcomers frequently believe, that list variables must be enclosed in brackets? | ||
People sometimes write argument patterns like <hask>[x]</hask>, hoping that <hask>x</hask> will assume all values of a list successively. | People sometimes write argument patterns like <hask>[x]</hask>, hoping that <hask>x</hask> will assume all values of a list successively. | ||
| - | + | Example: | |
| + | <haskell> | ||
| + | case xs of | ||
| + | [x] -> [f x] | ||
| + | </haskell> | ||
| + | although they meant | ||
| + | <haskell> | ||
| + | [f x | x <- xs] | ||
| + | </haskell> | ||
| + | or just <hask>map f xs</hask>. | ||
| + | It seems like they expect some kind of [[list comprehension]]. | ||
| + | However, if there wouldn't be a special [[list notation]] and if there wouldn't be the special syntax for the list type constructor (thus we must write <hask>[a]</hask> instead of <hask>List a</hask>), | ||
| + | then there would be certainly less confusion. | ||
| + | <!-- the first time I saw this in students homework solutions --> | ||
See for example | See for example | ||
| - | Haskell-Cafe about [http://www.haskell.org/pipermail/haskell-cafe/2008-April/041556.html Pattern match failure] | + | Haskell-Cafe about |
| + | [http://www.haskell.org/pipermail/haskell-cafe/2008-April/041496.html testing for same characters in lists of strings], | ||
| + | [http://www.haskell.org/pipermail/haskell-cafe/2008-April/041556.html Pattern match failure] | ||
[[Category:Syntax]] | [[Category:Syntax]] | ||
Current revision
Why do Haskell newcomers frequently believe, that list variables must be enclosed in brackets?
People sometimes write argument patterns like[x]
x
Example:
case xs of [x] -> [f x]
although they meant
[f x | x <- xs]
map f xs
It seems like they expect some kind of list comprehension.
However, if there wouldn't be a special list notation and if there wouldn't be the special syntax for the list type constructor (thus we must write[a]
List a
then there would be certainly less confusion.
See for example Haskell-Cafe about testing for same characters in lists of strings, Pattern match failure
