I am trying to finish up a simple homework assignment in Haskell for a class at my university, but I cannot figure out why my code won't compile:
-- Comments
module Main where
main :: IO ()
main = do
n <- readLn
print (fac n)
print (facList n)
print (sumFacs n)
print (fibonacci n)
-- Aufgabe 2 (a):
fac :: Int -> Int
let
fac 0 = 1
fac i = i * fac(i - 1)
-- Aufgabe 2 (b):
facList :: Int -> Int -> [Int]
let
facList x y = [fac m | m <- [x..y]]
sumFacs :: Int -> Int -> Int
let
sumFacs x y = sum (facList x y)
-- Aufgabe 3:
fibonacci :: Int -> Int
let
fibonacci 0 = 1
fibonacci 1 = 1
fibonacci i = fibonacci (i - 1) + fibonacci (i - 2)
When I attempt to compile the above code using the Glasgow compiler, I get the following error message:
Uebung01.hs:19:1: error:
parse error (possibly incorrect indentation or mismatched brackets)
|
19 | facList :: Int -> Int -> [Int]
| ^
All of the functions work in interactive mode. Sorry for posting such a simple question, but I am completely new to Haskell and am really struggling to understand how the whitespace rules work. I have looked at answers to similar questions, but I'm still unable to find my mistake. Thanks for reading.
A let
block [Haskell-report] expects an in
to specify expression. In your fac
function you define a let
block, but without an in
, this is used to define locally scoped variable(s) that you can then use in the in
clause. You however do not need a let
here, you can define fac
as:
fac :: Int -> Int
fac 0 = 1
fac i = i * fac (i - 1)
You need to refactor other functions in a similar manner.