Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re^16: World's shortest intro to function programming

by kelan (Deacon)
on Jun 21, 2005 at 14:44 UTC ( #468696=note: print w/replies, xml ) Need Help??

in reply to Re^15: World's shortest intro to function programming
in thread Thread on Joel on software forum : "I hate Perl programmers."

I'd just really like to see a reasonably simple but complete program developed from start to finish, by a competent Haskell programmer, as a way of getting a step up into the use of the language.

You may have seen this already, but nothingmuch was also fed-up with having no real-world examples in the Haskell tutorials. So to help himself learn, and maybe help out the other like-minded imperative programmers, he's developing a Forth compiler targetting Parrot, in a tutorial-like format. It starts off simple and builds up, so it's not too difficult to follow. Give it a look if you haven't already.

Simple example: When should I use data, when type or newtype?

The Anonymous Monk's reply to this question is clear and concise, but for anyone following along that doesn't know C, here's a more verbose answer.

type is just for creating a type-synonym. It doesn't really create a new type, but it can make your code easier to understand by giving a more meaningful name to an existing type. For example:

type Name = String lowerName :: Name -> Name lowerName = map toLower -- then: lowerName "KELAN" -- evaluates to "kelan"
In the above, the types Name and String can be used interchangeably, but using Name appropriately can make the code's intent clearer.

data is for defining a completely new type. This confused me at first, too, because the keyword data doesn't seem to relate to types. I'm guessing that the reason for that keyword is because this construct is used to define the data constructors for the type. Example:

data Piece = Pawn | Rook | Knight | Bishop | Queen | King -- The type is 'Piece', and to construct an actual value of -- that type, you use one of the six data constructors. promotesTo :: Piece -> [Piece] promotesTo Pawn = [ Rook, Knight, Bishop, Queen ] promotesTo _ = [] -- then: promotesTo Pawn -- evaluates to [Rook, Knight, Bishop, Queen] promotesTo $ head $ promotesTo Pawn -- evaluates to []
And data constructors can take parameters, and each can take different numbers of parameters:
data Rectangle = UnitSquare | Square Int | Rect Int Int -- examples of values for each would be: v1 :: Rectangle v1 = UnitSquare v2 :: Rectangle v2 = Square 3 v3 :: Rectangle v3 = Rect 5 9
Probably what confused me the most about the data construct when I started learning it was that the examples used the same word for the typename and the data constructor, so it was unclear that they are actually separate ideas.

newtype, as the Anonymous Monk said, is the same as data, except there can be only one data constructor, and it gets optimized away for lower overhead.

A reference I found helpful when learning about type and data was Tour of the Haskell Syntax, which gives the actual syntax in a clear and concise form.

Replies are listed 'Best First'.
Re^17: World's shortest intro to function programming
by BrowserUk (Patriarch) on Jun 21, 2005 at 20:17 UTC
    probably what confused me the most about the data construct when I started learning it was that the examples used the same word for the typename and the data constructor, so it was unclear that they are actually separate ideas.

    I think you hit the nail on the head. The use of single character identifiers--in the documentation, I don't have so much of a problem with their use for local vars in actual code--is a big barrier to understanding.

    If the docs (and maybe the type signatures generated by ghci) used somewhat more verbose identifiers (typeA instead of just a or funcA instead of f or p etc.), I think it would make it easier to assimilate.

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://468696]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2022-08-18 19:19 GMT
Find Nodes?
    Voting Booth?

    No recent polls found