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.


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.