Offside rule for function arguments?
Why doesn't Haskell allow something like this? fac 0 = 0 1 = 1 x = x * fac (x-1) This would be clearer than repeating the function name each time, and follow the same pattern as guards and case.
On Mon, Aug 23, 2010 at 09:33:13AM +0300, John Smith wrote:
Why doesn't Haskell allow something like this?
fac 0 = 0 1 = 1 x = x * fac (x-1)
This would be clearer than repeating the function name each time, and follow the same pattern as guards and case.
Good question. I don't know of any particular reason. -Brent
Maybe because of this: function 0 = 0 where fun 1 = 1 2 = 2 The last declaration (2=2) can define either fun or function. I'm not saying this is a major problem, but there may be other problems like these. /J On 23 August 2010 11:15, Brent Yorgey <byorgey@seas.upenn.edu> wrote:
On Mon, Aug 23, 2010 at 09:33:13AM +0300, John Smith wrote:
Why doesn't Haskell allow something like this?
fac 0 = 0 1 = 1 x = x * fac (x-1)
This would be clearer than repeating the function name each time, and follow the same pattern as guards and case.
Good question. I don't know of any particular reason.
-Brent _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
The indentation on the second line would generate a parse error, the same as it does now. On 23/08/2010 12:32, Jonas Almström Duregård wrote:
Maybe because of this:
function 0 = 0 where fun 1 = 1 2 = 2
The last declaration (2=2) can define either fun or function. I'm not saying this is a major problem, but there may be other problems like these.
/J
On 23 August 2010 11:15, Brent Yorgey <byorgey@seas.upenn.edu <mailto:byorgey@seas.upenn.edu>> wrote:
On Mon, Aug 23, 2010 at 09:33:13AM +0300, John Smith wrote:
Why doesn't Haskell allow something like this?
fac 0 = 0 1 = 1 x = x * fac (x-1)
This would be clearer than repeating the function name each time, and follow the same pattern as guards and case.
Good question. I don't know of any particular reason.
-Brent
The indentation on the second line would generate a parse error, the same as it does now. What parser error is that? Both
function 0 = 0 where fun 1 = 1 function 2 = 2 and function 0 = 0 where fun 1 = 1 fun 2 = 2 works for me. /J On 23 August 2010 11:46, John Smith <voldermort@hotmail.com> wrote:
The indentation on the second line would generate a parse error, the same as it does now.
On 23/08/2010 12:32, Jonas Almström Duregård wrote:
Maybe because of this:
function 0 = 0 where fun 1 = 1 2 = 2
The last declaration (2=2) can define either fun or function. I'm not saying this is a major problem, but there may be other problems like these.
/J
On 23 August 2010 11:15, Brent Yorgey <byorgey@seas.upenn.edu <mailto: byorgey@seas.upenn.edu>> wrote:
On Mon, Aug 23, 2010 at 09:33:13AM +0300, John Smith wrote:
Why doesn't Haskell allow something like this?
fac 0 = 0 1 = 1 x = x * fac (x-1)
This would be clearer than repeating the function name each time, and follow the same pattern as guards and case.
Good question. I don't know of any particular reason.
-Brent
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
Sorry, I missed the where. On 23/08/2010 12:52, Jonas Almström Duregård wrote:
The indentation on the second line would generate a parse error, the same as it does now. What parser error is that? Both
function 0 = 0 where fun 1 = 1 function 2 = 2
and
function 0 = 0 where fun 1 = 1 fun 2 = 2
works for me.
/J
On 23 August 2010 11:46, John Smith <voldermort@hotmail.com <mailto:voldermort@hotmail.com>> wrote:
The indentation on the second line would generate a parse error, the same as it does now.
On 23/08/2010 12:32, Jonas Almström Duregård wrote:
Maybe because of this:
function 0 = 0 where fun 1 = 1 2 = 2
The last declaration (2=2) can define either fun or function. I'm not saying this is a major problem, but there may be other problems like these.
/J
On 23 August 2010 11:15, Brent Yorgey <byorgey@seas.upenn.edu <mailto:byorgey@seas.upenn.edu> <mailto:byorgey@seas.upenn.edu <mailto:byorgey@seas.upenn.edu>>> wrote: > On Mon, Aug 23, 2010 at 09:33:13AM +0300, John Smith wrote: >> Why doesn't Haskell allow something like this? >> >> fac 0 = 0 >> 1 = 1 >> x = x * fac (x-1) >> >> This would be clearer than repeating the function name each time, >> and follow the same pattern as guards and case. > > Good question. I don't know of any particular reason. > > -Brent
On 08/23/10 02:33, John Smith wrote:
Why doesn't Haskell allow something like this?
fac 0 = 0 1 = 1 x = x * fac (x-1)
This would be clearer than repeating the function name each time, and follow the same pattern as guards and case.
Layout is detected and parsed when and only when it is preceded by 'where', 'let', 'do', or 'of'. So Haskell would have to have some such keyword to indicate "let the layout begin!" -- which could make it a bit uglier -- but no I don't know why. Sometimes where there are tons of cases I define the function using 'case' instead -- I probably wouldn't for your above example, but to show how it'd be written, fac x = case x of 0 -> 0 1 -> 1 _ -> x * fac (x-1)
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 8/23/10 14:56 , Isaac Dupree wrote:
On 08/23/10 02:33, John Smith wrote:
Why doesn't Haskell allow something like this?
fac 0 = 0 1 = 1 x = x * fac (x-1)
This would be clearer than repeating the function name each time, and follow the same pattern as guards and case.
Layout is detected and parsed when and only when it is preceded by 'where', 'let', 'do', or 'of'. So Haskell would have to have some such keyword to
I think the next question is "so how do guards work?" - -- brandon s. allbery [linux,solaris,freebsd,perl] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.10 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkxyxdoACgkQIn7hlCsL25VVqgCg0tLmPDFClCTgr1ExoSFZOMMT ri4AoM2MX9vRXMo0YHuiX4PIgPiGi/GV =l0lN -----END PGP SIGNATURE-----
On Monday 23 August 2010 21:02:50, Brandon S Allbery KF8NH wrote:
On 8/23/10 14:56 , Isaac Dupree wrote:
On 08/23/10 02:33, John Smith wrote:
Why doesn't Haskell allow something like this?
fac 0 = 0 1 = 1 x = x * fac (x-1)
This would be clearer than repeating the function name each time, and follow the same pattern as guards and case.
Layout is detected and parsed when and only when it is preceded by 'where', 'let', 'do', or 'of'. So Haskell would have to have some such keyword to
I think the next question is "so how do guards work?"
Not by layout: function :: Int -> Int -> Int -> Bool function x y z | even x = y > z | odd y = even (z-x) | otherwise = even z parses fine. Guards are introduced by the token `|', they need not be aligned, you can have multiple on the same line.
On 08/23/10 15:02, Brandon S Allbery KF8NH wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 8/23/10 14:56 , Isaac Dupree wrote:
On 08/23/10 02:33, John Smith wrote:
Why doesn't Haskell allow something like this?
fac 0 = 0 1 = 1 x = x * fac (x-1)
This would be clearer than repeating the function name each time, and follow the same pattern as guards and case.
Layout is detected and parsed when and only when it is preceded by 'where', 'let', 'do', or 'of'. So Haskell would have to have some such keyword to
I think the next question is "so how do guards work?"
Weirdly! It's a bit like "|" is a prefix operator, at least when found in this relevant parsing-context (and not e.g. in a list comprehension). The guards don't need to line up either. I admit that I wasn't too happy when I found out how guards parse. "|" looks way too symmetric of a character for it to do that... Linebreaks are not required! : fac x | x==0 = 0 | x==1 = 1 | otherwise = x * fac (x-1)
participants (6)
-
Brandon S Allbery KF8NH -
Brent Yorgey -
Daniel Fischer -
Isaac Dupree -
John Smith -
Jonas Almström Duregård