views:

328

answers:

5

I'm working on the 2nd Euler's problem, but for now I'm just trying to define the Fibonacci sequence. Currently, I have my Fib function defined as

Fib 0 = 0
Fib 1 = 1
Fib x = Fib (x - 1) + Fib (x - 2)

Because I want the program to depend on my keyboard input, I used

main = do
        putStrLn "Enter desired Fibonacci index: "
        "index" <- getLine
        putStrLn Fib x

And defined x as x = read "index"

I'm pretty sure I'm messing up both the indentation, and my syntax. Depending on how I modify the code, running "ghc /dir/file.hs gets me stuff like "Not in scope: data constructor `Fib'", or "parse error (possibly incorrect indentation)". I really, really don't know what the heck I'm doing, especially with regard to how to set up the .hs to be compiler friendly. I've read through about 7 tutorials on compiling with GHC, but they all seem to skip the part where they explain the basic requirements of compiling.

Please let me know how I can be more specific if that would help. I think the two things I'm trying to understand is how the whole "main" thing is related to compiling, and how I can get it to recognized I HAVE defined Fib as a function, even though it says its not in scope.

PS: I can't figure out the indentation here, Stack seems to want to put everything in one line, so I have to separate it all out. Sorry.

+6  A: 

The first letter of Fib should not be capitalized. Capitalized words indicate a type. Functions should start with a lower case letter.

The compiling looks fine!

You'll also get a few other errors when you compile. Remember that putStrLn takes a String as an argument. Your fib function returns an Integer. You can use show to convert a value into a String.

Jeff Foster
Just a note: capitalized identifiers indicate data constructors (as in this error) or type constructors (in type context) or modules names (in module context).
Don Stewart
+2  A: 

Don't bother with a main function. Just launch ghci, load your module, and invoke the fib function directly.

Barry Brown
+1  A: 

IIRC, getLine returns a string, so you should bind that to a variable

do
   idx <- getLine
   print $ fib $ read idx
Marcus Lindblom
That's not going to work, as fib takes a numeric value. The last line would have to be `putStrLn $ show $ fib $ read idx`. Also, you can use `print` for `putStrLn . show`.
yatima2975
Right you are. My Haskell is getting rusty.
Marcus Lindblom
+5  A: 

Just summing it up:

  • Your indentation looks ok.
  • Function names cannot begin with an uppercase letter. Capitalized identifiers are used for constructors, type and module names only.
  • You can test your stuff with an interactive interpreter and don't need a main function.
  • You need a variable name if you want to store the result of some computation, so the getLine result should be bind to a index, not "index". The first will be a string variable, not a string value.
  • putStrLn takes only one parameter (the string to be shown). In your program, it receives two parameters: a function Fib and some undefined x.
  • By the way, you need to define x, probably using read to convert the input string to an integer value.
  • Also, putStrLn needs a String and Fib returns an integer, you need to convert it before passing it in: putStrLn (show (Fib x)).
Thiago Arrais
+1  A: 

Here's your code, fixed so that you can actually compile it.

fib 0 = 0
fib 1 = 1
fib x = fib (x - 1) + fib (x - 2)

main = do
        putStrLn "Enter desired Fibonacci index: "
        index <- getLine
        print (fib (read index))

You had a couple of problem. Functions start with lower case letters. Secondly, you can't apply putStrLn to the result of fib because fib returns an integer and putStrLn expects a String. So I've used print instead. Also, you tried to use a string ("index") as a variable. It's true that you get a String from getLine but it has to be stored in a variable and a variable doesn't have quotes around it.

Note, just as Barry said, you don't really need the main function. Just put your fibonacci function in a file, load it into ghci and call the function from there. Easy peasy.

Also, note that your fibonacci function is horribly slow, but I hope you're already aware of that.

Good luck with the Euler problems. I've found it quite fun to use Haskell to solve them.

svenningsson