Home > Enterprise >  Compile Error in Haskell with recursive `let` function with `if then else`
Compile Error in Haskell with recursive `let` function with `if then else`

Time:09-29

UPD: Figured it out, I had to type ghci first in the terminal

enter image description here

Hello,

Why does this code:

let fact n = if n == 0 then 1 else n * fact(n - 1)

compile and run in this program in the image, but when I try to do the same, it says

[1 of 1] Compiling Main             ( main.hs, main.o )
main.hs:2:1: error:
    parse error (possibly incorrect indentation or mismatched brackets)

Has this syntax become outdated?

NOTE: I am using online gdb because I can't figure out how to install Haskell yet.

CodePudding user response:

compile and run in this program in the image, but when I try to do the same.

Because if you write a let, it expects an in … clause next. In a program, you define functions. You can use the main function that will run by default, so:

main :: IO ()
main = do
    let fact n = if n == 0 then 1 else n * fact (n - 1)
    print (fact 5)
  • Related