Re: [Haskell-cafe] About code style ?
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
Re: [Haskell-cafe] About code style ?
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 z_a...@163.com 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
RE: [Haskell-cafe] About code style ?
From: haskell-cafe-boun...@haskell.org [mailto:haskell-cafe-boun...@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. * ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] About code style ?
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 z_a...@163.com 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. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] About code style ?
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
Re: [Haskell-cafe] About code style ?
zaxis z_a...@163.com writes: 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.miljeno...@gmail.com IvanMiljenovic.wordpress.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] About code style ?
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/ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] About code style ?
However, i donot know how to write pure function using C style. func1 a = { -- ...; a * 2; -- ...; } What do you mean by a * 2? If you don't use this value, don't calculate it. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] About code style ?
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 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe