Reputation:
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.
Upvotes: 1
Views: 660
Reputation: 34160
Just summing it up:
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
.x
, probably using read
to convert the input string to an integer value.putStrLn
needs a String and Fib
returns an integer, you need to convert it before passing it in: putStrLn (show (Fib x))
.Upvotes: 5
Reputation: 4049
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.
Upvotes: 2
Reputation: 25680
IIRC, getLine
returns a string, so you should bind that to a variable
do
idx <- getLine
print $ fib $ read idx
Upvotes: 1
Reputation: 20604
Don't bother with a main function. Just launch ghci, load your module, and invoke the fib function directly.
Upvotes: 1
Reputation: 44696
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
.
Upvotes: 9