
For me i like C style instead of layout. For example, func1 a = do -- ... a * 2 -- ... I always write it as: func1 a = do { -- ...; a * 2; -- ...; } However, i donot know how to write pure function using C style. func1 a = { -- ...; a * 2; -- ...; } will not compile without `do`. Sincerely! ----- fac n = foldr (*) 1 [1..n] -- View this message in context: http://old.nabble.com/About-code-style---tp27414627p27414627.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

zaxis
However, i donot know how to write pure function using C style. func1 a = { -- ...; a * 2; -- ...; }
You mean imperatively? Short answer: you can't and you shouldn't. Slightly longer answer: you can possibly fudge something together using the Identity monad from mtl, but that will involve wrapping/unwrapping everywhere. Learn to think about how to chain/group functions together to form more of a "pipeline" rather than a sequence of statements. Haskell =/= C. -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

zaxis wrote:
For me i like C style instead of layout. For example, func1 a = do -- ... a * 2 -- ...
I always write it as: func1 a = do { -- ...; a * 2; -- ...; }
Honestly, don't do this. When you're coding in Haskell you should write idiomatic Haskell and when doing C, do idiomatic C. Inventing your own coding style for a language will make it difficult for other people who know and use that language to read your code and sooner or later you will want to or need to work with others. Erik -- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/

Hello zaxis,
as others have noted, you are trying to write C in Haskell. Well, that
gains you nothing and rather gets you into trouble. If you want to
write C, then use instead and not Haskell, because the idioms you're
used to in C will not work at all in Haskell. Note for example that the
'do' keyword is misleading. It does not introduce imperative code, but
monadic code, which may well be pure.
To answer your question: There are no blocks in Haskell, which you
could put into braces. Don't think in blocks of code, because that is
C, not Haskell, and it's plain wrong. You are not going to write any
for/while loops anyway. Thinking that way may have a negative impact on
your Haskell coding style. The only place where braces are used is
record types and many Haskell programmers (including me) consider this
an ugly syntax.
But don't worry, you'll get used to the new syntax and eventually fall
in love with it, because it will make your life easier. I myself came
from years of C/C++ experience and had similar difficulties in the
beginning.
Greets
Ertugrul
zaxis
For me i like C style instead of layout. For example, func1 a = do -- ... a * 2 -- ...
I always write it as: func1 a = do { -- ...; a * 2; -- ...; }
However, i donot know how to write pure function using C style. func1 a = { -- ...; a * 2; -- ...; }
will not compile without `do`.
Sincerely!
----- fac n = foldr (*) 1 [1..n]
-- nightmare = unsafePerformIO (getWrongWife >>= sex) http://blog.ertes.de/

Ditto what everyone else has said. But to clarify what's going on: The braces are used to introduce a list of "things", such as monadic actions, data fields, or declarations. For example, consider the following code: f a = let { a_times_2 = a*2; a_times_4 = a*4; } in a_times_2+a_times_4 main = putStrLn $ "f 3 = " ++ show (f 3) The reason why my code compiled and yours didn't is because the compiler saw that the braces were being used to introduce a list of declarations, and the reason why it knew this was because of the "let" keyword. By contrast, in your code it doesn't see a "let", so it assumes that you must be introducing a list of monadic actions. Hence it yells at you for not putting in a "do". Remember that a pure function is merely a definition of what the output is for a given input. It does not say anything about *how* to do this. Thus, you should never think of a pure function as being a list of actions but rather (approximately) a definition which may require some additional declarations (such as introduced by "let") solely for the purpose of making it easier for *you* to *express* what its value is. (I say approximately because the way you express it does affect the way it gets computed despite technically being pure, but this is not something you should be worrying about right now.) But again, even though you could use curly brackets and semicolons as I illustrated above, you really should be using whitespace as it is the standard practice; others reading your code may be confused by their presence and so have to work harder to figure out what is going on. Cheers, Greg On Feb 1, 2010, at 6:22 PM, zaxis wrote:
For me i like C style instead of layout. For example, func1 a = do -- ... a * 2 -- ...
I always write it as: func1 a = do { -- ...; a * 2; -- ...; }
However, i donot know how to write pure function using C style. func1 a = { -- ...; a * 2; -- ...; }
will not compile without `do`.
Sincerely!
----- fac n = foldr (*) 1 [1..n] -- View this message in context: http://old.nabble.com/About-code-style---tp27414627p27414627.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

thanks for all suggestions. zaxis wrote:
For me i like C style instead of layout. For example, func1 a = do -- ... a * 2 -- ...
I always write it as: func1 a = do { -- ...; a * 2; -- ...; }
However, i donot know how to write pure function using C style. func1 a = { -- ...; a * 2; -- ...; }
will not compile without `do`.
Sincerely!
----- fac n = foldr (*) 1 [1..n] -- View this message in context: http://old.nabble.com/About-code-style---tp27414627p27416932.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

fac n = let {
f = foldr (*) 1 [1..n]
} in f
:D
sorry for double reply, need to cc cafe, this is fun.
On Tue, Feb 2, 2010 at 4:33 PM, zaxis
thanks for all suggestions.
zaxis wrote:
For me i like C style instead of layout. For example, func1 a = do -- ... a * 2 -- ...
I always write it as: func1 a = do { -- ...; a * 2; -- ...; }
However, i donot know how to write pure function using C style. func1 a = { -- ...; a * 2; -- ...; }
will not compile without `do`.
Sincerely!
----- fac n = foldr (*) 1 [1..n] -- View this message in context: http://old.nabble.com/About-code-style---tp27414627p27416932.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- jinjing

fac n = let { f = foldr (*) 1 [1..n] } in f VERY interesting :) Jinjing Wang wrote:
fac n = let { f = foldr (*) 1 [1..n] } in f
:D
sorry for double reply, need to cc cafe, this is fun.
On Tue, Feb 2, 2010 at 4:33 PM, zaxis
wrote: thanks for all suggestions.
zaxis wrote:
For me i like C style instead of layout. For example, func1 a = do -- ... a * 2 -- ...
I always write it as: func1 a = do { -- ...; a * 2; -- ...; }
However, i donot know how to write pure function using C style. func1 a = { -- ...; a * 2; -- ...; }
will not compile without `do`.
Sincerely!
----- fac n = foldr (*) 1 [1..n] -- View this message in context: http://old.nabble.com/About-code-style---tp27414627p27416932.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- jinjing _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
----- fac n = foldr (*) 1 [1..n] -- View this message in context: http://old.nabble.com/About-code-style---tp27414627p27429649.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@haskell.org] On Behalf Of zaxis
For me i like C style instead of layout. For example, func1 a = do { -- ...; a * 2; -- ...; }
The report has all the gory details. The brace+semicolon syntax isn't just for do-blocks; it can be used anywhere that indenting is used to specify scope. Section 2.7: http://www.haskell.org/onlinereport/lexemes.html#lexemes-layout Section 9.3: http://www.haskell.org/onlinereport/syntax-iso.html#layout Alistair ***************************************************************** Confidentiality Note: The information contained in this message, and any attachments, may contain confidential and/or privileged material. It is intended solely for the person(s) or entity to which it is addressed. Any review, retransmission, dissemination, or taking of any action in reliance upon this information by persons or entities other than the intended recipient(s) is prohibited. If you received this in error, please contact the sender and delete the material from any computer. *****************************************************************
participants (8)
-
Bayley, Alistair
-
Erik de Castro Lopo
-
Ertugrul Soeylemez
-
Gregory Crosswhite
-
Ivan Lazar Miljenovic
-
Jinjing Wang
-
Miguel Mitrofanov
-
zaxis