[Haskell-beginners] Re: question on layout

Maurí­cio briqueabraque at yahoo.com
Thu Jun 18 13:20:39 EDT 2009


>> If you want to start a page in Haskell wiki with a reference
>> to layout-free Haskell syntax I would be happy to contribute.

> Since you have some experience with it, why don't you start
 > the page to lead newbies from C++-land in? You hopefully
 > remember a few things that were especially hard to learn for
> you, that could probably help a lot.

I tought about that, but you're the first one to actually agree
with me about that :) Now I would not like to start something I
know I won't take care of, since I do take care of other stuff.
But I can add stuff if somebody else does. Note that it would
be great if instead of just a layout free reference we had
a full common language version of the technical Haskell 98
specification. If you are learning Haskell, doing that can be
really great as a learning tool:

http://haskell.org/onlinereport

Many words and explanations seen there are important to
understand today version of Haskell, and most are not well
explained by tutorials. You won't find about "kinds" in most
tutorials, for instance, and they are really important.


Here is a small reference (improvised, not checked, and 'classes'
and their instances are important and missing):

* How to write modules. Today, all Haskell compilers
needs you to write only one module per file:

module Name.OtherName (export_list) where {

declarations separate by ';'

}

declarations may be imports (and those come first than
all others):

import ModuleName.SubModule qualified as W ;
import OtherModule ;
etc.

export_list lists all names you want to be seen by other
modules that import Name.OtherName. (This requires further
details.) If you are writing module Main, which all
applications are required to have, you need to export at
least 'main'.

* 'do' notation

do { x ; y'<- y ; z' <- z y' ; a }

expands to

x >> y >>= \y' -> z y' >>= \z' -> a

Also, using layout,

do
      a <- x
      let b = a
      y b
      z

expands to

do {a <- x ; let {b = a} in do {y b >> z}}

and then to

x >>= \a -> let {b=a} in y b >> z



* Using '::'

You can declare types for names using :: like:

a :: IO ()
b :: Integer

(Needs a lot of further details.)

These appear in declarations list of modules:

module Bla (ble) where {
   import SomeModule ; ble :: Integer ; ble = 3
}


* Using 'data'

You can declare data types as:

data DataName = Constructor1 Integer | Cons2 String
     | Cons3 Integer String DataName


* Using '=' and pattern matching

A somewhat general usage of '=' can be seen as (using
that data type from previous item):

f :: SomeType -> DataName -> Bool

  ;

f (Constructor a b) c = case c of {
     Constructor1 i
          | h i -> True
          | True -> False where { h :: Integer -> Bool ; h = (>= 10) }

       ;

     Cons2 s | matches s -> True | otherwise -> False

       ;

     Cons3 j _ = j >= k

} where { k :: Integer ; k = 5 }


* Using 'let' and 'where'

'let' is different from 'where'. You can do:

a = (let {b=5} in b + b) + (let {b=6;c=7} in b + c)

but not

a = (b + b where {b=5}) etc. (WRONG!!)

'where' is part of '=':

a = b 9
   where {b :: Integer -> String ; b 10 = "Equals ten." ; b _ = "Does not equal ten."}


* Lazy evaluation:

b :: Integer -> String ;

b 10 = "Ten" ;

b 9 = "Nine" ;

func :: (Integer -> String) -> String ;

func f = "Prefix" ++ f 8


Here, 'take 2 (func b)' (or 'take 2 $ func b')
would evaluate to "Pr" dispite 'b 8' beeing undefined,
because 'f 8' is not necessary to evaluate just the first
two characters of that string.

Hope this gives you some help, and contains only a few errors.

Best,
Maurício



More information about the Beginners mailing list