Why does Haskell so often seem to treat [] as a general null.
For example I know 0 : 1 : [] gives [0, 1]. But shouldn't it produce a type fault in a consistent world? Int:Int:List isn't properly a list. It mixes types. I expect something like: Let GSN mean general_scalar_null. 1 : 2 : GSN -- works 1 : 2 : [] -- type fault, cannot mix int and empty list in the same list. And why does [] == [] : [] instead of [[], []] == [] : [] What sorts of nullity are there in core Haskell? Trent. _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
If you consider the type of the operator (:) you have: Prelude> :t (:) (:) :: a -> [a] -> [a] So it takes an element of type a and a list. So if you write 1:[2,3,4] the type is correct, because you give an integer 1 and a list of integers [2,3,4]. You will obtain the list of integers [1,2,3,4]. Similarly, writing 1:[] is correct and gives you [1] as result. Then, if you write 0 : 1 : [] (as in your example), is the same as 0 : (1 : []) so it means 0 : [1], which is [0,1]. So, the operator (:) is right associative. If it was left associative, your example would give an error. Indeed (0 : 1) : [] is not correct in Haskell. Furthermore, your final examples are both false: Prelude> [] == [] : [] False [[], []] == [] : [] False The following is True: Prelude> [[]] == [] : [] True Indeed if you write [] : [] youy mean you want to build a list whose first element (head) is [] and whose "tail" (i.e. the rest of the list) is the empty list. So, if 1:[] is [1], then []:[] is [[]]. Ut 2018-08-18 11:13 GMT+02:00 trent shipley <[hidden email]>:
_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
In reply to this post by trent shipley
Hello Trent,
On Sat, Aug 18, 2018 at 02:13:25AM -0700, trent shipley wrote: > Why does Haskell so often seem to treat [] as a general null. > > For example I know 0 : 1 : [] gives [0, 1]. > > But shouldn't it produce a type fault in a consistent world? > > Int:Int:List isn't properly a list. It mixes types. `:` is not syntactic sugar, but a data constructor and behaves like one! λ> :type (:) (:) :: a -> [a] -> [a] "Give me an `a` and a list of `a`, I will return a list." The `empty list` ([]) is polymorphic: λ> :t [] [] :: [a] (it could be an empty list of strings, of ints, of dromedaries), so `3:[]` is well typed. Note that `3:[]:4` will not type-check and that to build a list, you *have* to start with a `[]`. _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
OK. That makes total sense. And a little experimentation with GHCi or reading the prelude would have prevented my spamming the list. What about the tacked on question about nullity in "core" Haskell? On Sat, Aug 18, 2018 at 2:37 AM Francesco Ariis <[hidden email]> wrote: Hello Trent, _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
The nullary constructor, as far as the term has meaning in most languages, is () a.k.a. unit. It takes no arguments, and returns a valid data type. There is, however, an even less populated type: Void has no legal values at all! If nullary is arity 0, this is roughly arity i. It is still useful as a type-level encoding of "impossible outcome" in parametric code. On Sat, Aug 18, 2018, 3:09 AM trent shipley <[hidden email]> wrote:
_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |