[Haskell-cafe] Re: New to Haskell

Jon Fairbairn jon.fairbairn at cl.cam.ac.uk
Tue Dec 18 09:49:22 EST 2007


"Cristian Baboi" <cristi at ot.onrc.ro> writes:

> On Tue, 18 Dec 2007 11:56:36 +0200, Jon Fairbairn
> <jon.fairbairn at cl.cam.ac.uk> wrote:
>
>> "Cristian Baboi" <cristi at ot.onrc.ro> writes:
>
>
>>> - the syntax for a block
>>
>> Not sure what you mean by "block".
>>
>> do a <- [1..10]
>>    b <- [3,4]
>>    return (a,b)
>>
>> is an expression... you can write that same expression as
>> do {a <- [1..10]; b <- [3,4]; return (a,b)} too.
>
>
> I mean anything that you can put between "{" "}", and between ";"

That's a bit like asking for the syntax of anything you can
put between "(" and ")"; The braces are used for grouping,
and can group different things:  

case 2 of {1 -> 2 ; 2 -> 2}
do {a <- Just 1; return a}

> Is this ([1 ,2 ,3 ,4]) a tuple or what ?
> It has commas in it!

Not in any meaningful sense...

>>> - what guarantees are made by the LANGUAGE that an IO action
>>> (such as  do  putStrLn "Hello world" ) is not performed
>>> twice
>
>> As has been pointed out, «do putStrLn "Hello world"» is an
>> expression that you can bind to a variable and use as many
>> times as you like.
>
> Yes, but that was not the question.
> What make you so sure it will be printed the exact number of
> times you  intended ?

I don't understand your question at all, then.  How many
times it gets printed depends on how many times the
programme is run, for one thing. Otherwise, it's a matter of
the definition of the semantics of the language.  Evaluation
of a Haskell programme proceeds from evaluation of «main»,
which returns an object of type IO -- a sequence of
Input/Output operatens -- that is "run". IO doesn't happen
when you evaluate an IO action, it happens when the IO
action is run. For example, if you define

f x = seq (putStrLn "foo!") (x+1)

and have 

main = print (f 2)

the «putStrLn "foo!"» is evaluated because seq forces its
first argument, but the only output you get is 3.


>> This is a fundamental property of the language.  A lambda
>> expression is programme and at runtime the system doesn't
>> know one lambda expression from another (all it can do with
>> one is apply it to something).
>
> Even C can apply a function variable to an argument (function pointers).

The secret of good language design is not what the language
allows, it's what the language forbids.

-- 
Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk




More information about the Haskell-Cafe mailing list