Haskell
Posted on September 28, 2006
I do not understand Haskell,
First: in one place we use [a] to define list of any types, in another we use _ as any type
Second from the Haskell faq:
…
data Color = Red | Green | Blue | Indigo | Violet
…
Here is an example of a type with just one data constructor:data Point a = Pt a a
Because of the single constructor, a type like Point is often called a tuple type, since it is essentially just a cartesian product (in this case binary) of other types. (Tuples are somewhat like records in other languages.) In contrast, multi-constructor types, such as Bool and Color, are called (disjoint) union or sum types.
More importantly, however, Point is an example of a polymorphic type: for any type t, it defines the type of cartesian points that use t as the coordinate type. The Point type can now be seen clearly as a unary type constructor, since from the type t it constructs a new type Point t. (In the same sense, using the list example given earlier, [] is also a type constructor. Given any type t we can “apply” [] to yield a new type [t]. The Haskell syntax allows [] t to be written as [t]. Similarly, -> is a type constructor: given two types t and u, t->u is the type of functions mapping elements of type t to elements of type u.)
Note that the type of the binary data constructor Pt is a -> a -> Point a, and thus the following typings are valid:
Pt 2.0 3.0 :: Point Float
Pt ‘a’ ‘b’ :: Point Char
Pt True False :: Point Bool
So for Colour user data type is on the left but for Point it is on the Right and is called Pt? What the fuck is Pt anyway?
Why cant we simply write Point x = (x,x) or something like that?
Thrid: why I do I have to write code in the text file, load it in the interpreter and run it but I can not enter data directly into interpreter?
Filed Under Uncategorized |
Leave a Comment
If you would like to make a comment, please fill out the form below.
Pt is an arbitrary data constructor. Pt could’ve been anything that started with a capital letter.
a != _
Well, only sort of.
a defines something of polymorphic type, i.e. anything. It’s useful because if we want to define something that takes 2 anythings but those anythings must be of the same type then we use a a.
_ on the other hand matches anything but doesn’t bind what it matched to anything. _ is basically saying I don’t care what I’m given give me anything. It’s used in pattern matching, not in function definitions.
So writing something like
helloes :: [a] -> a
helloes (x:xs) = x
is valid but
helloes2 :: [_] -> (a or _)
is not
The problem with a lot of Haskell documentation is that Haskell is a language written by academics so the documents is written by academics also. A lot of the documentation reads like a paper so it can get pretty confusing (like in what you copied above).
If you want a good tutorial that isn’t super dense (I think that comes from a gentle introduction right? It looks really familiar) try Yet Another Haskell Tutorial. It’s linked on the course page I put up and it’s by far the tutorial I’ve read that I like best.
Well I sort of understand know how they implemented constructors.
Basically a type can have many named constructors.
and it works comething like that:
Type a b = Const1 a | Const 2 b
Type is a datatype.
Const is a datatype constructor.
a and b are value types i.e they are value that Type holds. More over a and b are of different types.
So Const 1 and Const 2 are two constructors (each take a paramater of a different type) that create a Type.
Hey, maybe you can help me with this:
*Main> :t [ (getCard), (getCard), (getCard), (getCard), (getCard)]
[ (getCard), (getCard), (getCard), (getCard), (getCard)] :: [IO Card]
So basically I have a list of IO Card (they are IO because that a picked randomly from the deck, right?)
How can I print them one by one?
heh this works:
makeHand :: IO [(CardValue, Suit)]
makeHand = do
tell “Start making hand”
c1 < - getCard deck
c2 < - getCard (delete c1 deck)
c3 < - getCard (delete c2 (delete c1 deck))
c4 < - getCard (delete c3 (delete c2 (delete c1 deck)))
c5 < - getCard (delete c4 (delete c3 (delete c2 (delete c1 deck))))
return [ c1, c2, c3, c4, c5]