Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (8)
As of 2024-04-18 08:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found