[Haskell-beginners] (no subject)
David McBride
toad3k at gmail.com
Thu Mar 28 20:58:47 CET 2013
There are types and constructors. If "int i = 2"; is a statement, int
is the type, and 2 is the constructor. You can't put 2 on the left
side and you can't put int on the right side, it wouldn't work. The
way I like to think of it is that there are types, and then there are
values. Each value has a type. Each type can be represented by
different values. Types are used for type info and values that are
used for code. When you are declaring a data type it could be
composed of both pieces, and sometimes they are named the same, which
is where your confusion comes from.
data blahtype = blah | blah2 | blah3 int | blah4 Char Int | blahtype
The first blahtype is the type, the others are constructors. That
last blahtype is a constructor, not a type. It is completely legal to
have the same name as its type. The char and int are of course types.
fooFunc :: JSObject -> JSValue -> IO JSValue -- <- all these are types
fooFunc (JSONObject blah) (JSBool bvalue) = return $ JSObject
(JSONObject JSNull) -- <- all these are value related
It is very easy to see when writing functions, but when writing data
types it takes a bit of getting used to because it requires both
aspects at the same time.
data JSValue = ... | JSObject (JSObject JSValue) | ...
The first JSValue is a type. The first JSObject is a constructor.
The second JSObject is a type and so is the second JSValue. Put them
together it is a single type "JSObject JSValue". So that JObject
constructor takes a single type of (JSObject JSValue).
newtype JSObject e = JSONObject [(String, e)]
The first JSObject is again a type. The e will be replaced with a
type. JSONObject is a constructor again. The arguments to JSONObject
are types (String, and whatever e is). Constructors are essentially
the first word in each possible value. They are used to pattern match
and to construct new values.
I realize it looks confusing, but it becomes second nature after a little while.
On Thu, Mar 28, 2013 at 3:22 PM, Jamie F Olson <jamie.f.olson at gmail.com> wrote:
> Daniel Trstenjak <daniel.trstenjak <at> gmail.com> writes:
>
>> Ups, sorry, the data constructor to get a JSValue from a JSObject is
>> named - well - also JSObject.
>>
>> So it's:
>>
>> getJSObj $ JSObject a
>>
>> There's a data type named JSObject:
>>
>> newtype JSObject e = JSONObject { fromJSObject :: [(String, e)] }
>> deriving (Eq, Ord, Show, Read, Typeable )
>>
>> And there's a data constructor with the name JSObject for the data type
> JSValue:
>>
>> data JSValue
>> = ...
>> | JSObject (JSObject JSValue)
>> deriving (Show, Read, Eq, Ord, Typeable)
>>
>> Greetings,
>> Daniel
>>
>>
>
> Thanks. I'd seen that mentioned above, but I'm a bit new to Haskell and didn't
> really understand what that meant. In fact, I still don't. Do you have any
> good resources on that? None of the tutorials/manuals were entirely clear to
> me, nor was the wiki(http://www.haskell.org/haskellwiki/Constructor), which uses
> the same statement as the example of both type and data constructors.
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
More information about the Beginners
mailing list