Computer Science Canada Haskell telling me code isn't indented properly |
Author: | DtY [ Fri Apr 24, 2009 2:21 pm ] | ||||
Post subject: | Haskell telling me code isn't indented properly | ||||
So I decided I'd check out Haskell today, and I was following the wikibook on it. This seems to be how they're doing it over there, however, when I try to compile it, I get the error:
And I cannot figure out why. Any insight?[/code] |
Author: | bbi5291 [ Fri Apr 24, 2009 3:11 pm ] |
Post subject: | Re: Haskell telling me code isn't indented properly |
ghci rejects the line main >:: IO (). However, as it seems to work on your implementation (Hugs, I suppose) I guess you can leave it. In Haskell an if statement has to have both the then part and the else part. This is because an if statement is actually supposed to be an expression, which has to have a value; if the else part does not exist then it is not guaranteed that a value is returned at all. |
Author: | DtY [ Fri Apr 24, 2009 3:20 pm ] | ||
Post subject: | RE:Haskell telling me code isn\'t indented properly | ||
I really have no idea where that > is coming from, the syntax highlighter seems to have put it in, If you press the quote button, it's not there in the post. Odd. Anyway, I tried adding an else, and am still getting the same error.
|
Author: | bbi5291 [ Fri Apr 24, 2009 3:25 pm ] |
Post subject: | Re: Haskell telling me code isn't indented properly |
There is a quirk of Haskell that many consider a bug and may be "fixed" in later versions. When you have an if statement inside a do block, the then and else parts have to be indented further than the if. This is because otherwise they will be parsed as subsequent statements within the do block. This is not necessary when you are not inside a do block (in this case the then and else only have to be indented at least as much as the if). The position of the "then" is fine (after all, it is to the right of "if") but "else" needs to be moved to the right. |
Author: | Prabhakar Ragde [ Fri Apr 24, 2009 3:30 pm ] |
Post subject: | RE:Haskell telling me code isn\'t indented properly |
Indent the else past the if. This is a horrible first Haskell program. Work through Real World Haskell instead. You shouldn't be using I/O until you know a lot more. |
Author: | DtY [ Fri Apr 24, 2009 3:35 pm ] |
Post subject: | RE:Haskell telling me code isn\'t indented properly |
Okay, thanks, working now. This isn't my first program, I did the hello world and stuff. I'm not quite going through the book in perfect order, so I already do have a basic understanding of what that main :: IO () line does. Also, I seem to remember reading a while ago (before I started learning Haskell) that you could also use C style syntax with it (like { starts a block, } ends it, rather than based on indentation) is this true? If so, are there any short tutorials that use that style so I can get the idea of it? The indentation for blocks really bugs me. |
Author: | Prabhakar Ragde [ Fri Apr 24, 2009 4:11 pm ] |
Post subject: | RE:Haskell telling me code isn\'t indented properly |
It's in the Wikibook, under Indentation. Why don't you just write C programs instead? |
Author: | Prabhakar Ragde [ Fri Apr 24, 2009 4:16 pm ] |
Post subject: | RE:Haskell telling me code isn\'t indented properly |
Also see the Haskell Report. |
Author: | DtY [ Fri Apr 24, 2009 7:05 pm ] |
Post subject: | Re: RE:Haskell telling me code isn\'t indented properly |
Prabhakar Ragde @ Fri Apr 24, 2009 4:11 pm wrote: It's in the Wikibook, under Indentation.
Why don't you just write C programs instead? Thanks, I just thought I'd learn a new language. I've tried learning C++, but didn't get too far. Maybe I'll play with C this weekend. The reason I chose Haskell was because there were two featured wikibooks about it, and once I started reading it it interested me that variables were all static, and people weren't complaining. |
Author: | wtd [ Fri Apr 24, 2009 8:51 pm ] |
Post subject: | Re: RE:Haskell telling me code isn\'t indented properly |
DtY @ Sat Apr 25, 2009 8:05 am wrote: Prabhakar Ragde @ Fri Apr 24, 2009 4:11 pm wrote: It's in the Wikibook, under Indentation.
Why don't you just write C programs instead? Thanks, I just thought I'd learn a new language. I've tried learning C++, but didn't get too far. Maybe I'll play with C this weekend. The reason I chose Haskell was because there were two featured wikibooks about it, and once I started reading it it interested me that variables were all static, and people weren't complaining. You're going to be even more shocked when you figure out that there are no variables. |
Author: | Insectoid [ Fri Apr 24, 2009 9:15 pm ] |
Post subject: | RE:Haskell telling me code isn\'t indented properly |
They're all constants, no? I think it's ridiculous that indentation must be proper to compile/interpret in Haskell. It's ridiculous for any language. |
Author: | Prabhakar Ragde [ Fri Apr 24, 2009 9:25 pm ] |
Post subject: | RE:Haskell telling me code isn\'t indented properly |
Those Wikibooks do things in a strange order. The "Scheme" one is definitely aimed at people who already know a lot about functional programming. In learning a language like Haskell, you need to forget a lot of what you know about other languages. Let me again recommend Real World Haskell, which at least starts out properly. I haven't had the time to get very far into it, but none of the Haskell books that I have read and learned from are available free online. http://book.realworldhaskell.org/read/ The tutorial titled "A Gentle Introduction to Haskell" may also be worthwhile. |
Author: | wtd [ Fri Apr 24, 2009 9:33 pm ] | ||||
Post subject: | Re: RE:Haskell telling me code isn\'t indented properly | ||||
insectoid @ Sat Apr 25, 2009 10:15 am wrote: They're all constants, no?
I think it's ridiculous that indentation must be proper to compile/interpret in Haskell. It's ridiculous for any language. It doesn't have to be. Does make things easier, though. Look at C, for instance. It's free form. You can do anything you want with indentation and it doesn't care. How often do you see code like the following?
Vs.
|
Author: | DtY [ Fri Apr 24, 2009 10:39 pm ] |
Post subject: | Re: RE:Haskell telling me code isn\'t indented properly |
wtd @ Fri Apr 24, 2009 8:51 pm wrote: DtY @ Sat Apr 25, 2009 8:05 am wrote: Prabhakar Ragde @ Fri Apr 24, 2009 4:11 pm wrote: It's in the Wikibook, under Indentation.
Why don't you just write C programs instead? Thanks, I just thought I'd learn a new language. I've tried learning C++, but didn't get too far. Maybe I'll play with C this weekend. The reason I chose Haskell was because there were two featured wikibooks about it, and once I started reading it it interested me that variables were all static, and people weren't complaining. You're going to be even more shocked when you figure out that there are no variables. That's what I meant by static, they can't be changed. (I looked at C today, and I remembered static meant something else) |
Author: | Prabhakar Ragde [ Sat Apr 25, 2009 8:36 am ] |
Post subject: | Re: RE:Haskell telling me code isn\'t indented properly |
wtd @ Fri Apr 24, 2009 9:33 pm wrote: How often do you see code like the following?
Speaking as an instructor... way too often. It's clear that indentation makes programs more readable and maintainable. From there to putting it to semantic use is a short and not at all ridiculous step. Of course, it is possible to go too far (I will not name names here) and there should be ways to override the defaults when necessary. |
Author: | wtd [ Sat Apr 25, 2009 12:27 pm ] |
Post subject: | Re: RE:Haskell telling me code isn\'t indented properly |
Prabhakar Ragde @ Sat Apr 25, 2009 9:36 pm wrote: wtd @ Fri Apr 24, 2009 9:33 pm wrote: How often do you see code like the following?
Speaking as an instructor... way too often. Well, yes. I suppose I should have qualified that with "from people who know better." |
Author: | [Gandalf] [ Sat Apr 25, 2009 1:29 pm ] |
Post subject: | RE:Haskell telling me code isn\'t indented properly |
I think you misinterpreted his statement, wtd, as I did at first. I assume Python is being implied of taking things too far with indentation? |