Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Haskell telling me code isn't indented properly
Index -> Programming, General Programming -> Functional Programming
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DtY




PostPosted: Fri Apr 24, 2009 2:21 pm   Post subject: Haskell telling me code isn't indented properly

Haskell:
module Main where
import System.Environment

main >:: IO ()
main = do
    putStrLn "What is your name? "
    name <- getLine
    if name=="John" then
        putStrLn("HI JOHN!")

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:
code:
[jeff@localhost Greet]$ ghc -o greet greet.hs

greet.hs:10:0: parse error (possibly incorrect indentation)

And I cannot figure out why. Any insight?[/code]
Sponsor
Sponsor
Sponsor
sponsor
bbi5291




PostPosted: 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.
DtY




PostPosted: 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.

Haskell:
module Main where
import System.Environment

main >:: IO ()
main = do
    putStrLn "What is your name? "
    name <- getLine
    if name=="John" then
        putStrLn("HI JOHN!")
    else
        putStrLn("Hi someone else")
bbi5291




PostPosted: 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.
Prabhakar Ragde




PostPosted: 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.
DtY




PostPosted: 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.
Prabhakar Ragde




PostPosted: 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?
Prabhakar Ragde




PostPosted: Fri Apr 24, 2009 4:16 pm   Post subject: RE:Haskell telling me code isn\'t indented properly

Also see the Haskell Report.
Sponsor
Sponsor
Sponsor
sponsor
DtY




PostPosted: 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.
wtd




PostPosted: 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. Smile
Insectoid




PostPosted: 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.
Prabhakar Ragde




PostPosted: 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.
wtd




PostPosted: 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?

code:
int main() {
int i;
for (i = 0; i < 10; i++) {
printf("%d: Hello\n", i);
}
}


Vs.

code:
int main()
{
    int i;
    for (i = 0; i < 10; i++)
    {
        printf("%d: Hello\n", i);
    }
}
DtY




PostPosted: 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. Smile

That's what I meant by static, they can't be changed. (I looked at C today, and I remembered static meant something else)
Prabhakar Ragde




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, General Programming -> Functional Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 17 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: