Haskell - cannot run simple prgram

Viewed 78

I'm attempting to run a simple Haskell program.

Here is the code:

{-# LANGUAGE OverloadedStrings #-}
module Lib
    ( someFunc
    ) where

import qualified Data.Text.IO as T

someFunc :: IO ()
someFunc = T.putStrLn "someFunc1"

lucky :: (Integral a) => a -> String
lucky 7 = "LUCKY NUMBER SEVEN!"

let a = lucky 7

putStrLn a

When I attempt to compile the program using: stack build

The error is :

    parse error (possibly incorrect indentation or mismatched brackets)
   |
16 | putStrLn a
   | ^

I'm following the guide at http://learnyouahaskell.com/chapters but it is all based on using REPL, whereas I want to produce reproducible results in the IDE. I'm using the Haskell IDE for IntelliJ and created the project using stack new first-project following guide from https://docs.haskellstack.org/en/stable/GUIDE/ .

How to compile and run this Haskell program ?

Update :

Using the suggested answer from pedrofurla the modified code is :

{-# LANGUAGE OverloadedStrings #-}
module Lib
    ( someFunc
    ) where

import qualified Data.Text.IO as T

someFunc :: IO ()
someFunc = T.putStrLn "someFunc1"

lucky :: (Integral a) => a -> String
lucky 7 = "LUCKY NUMBER SEVEN!"

a = lucky 7

main = putStrLn a

The code now compiles ( using stack build ) .

On running the code ( using stack exec first-project-exe )it just outputs "someFunc1" . I expect "LUCKY NUMBER SEVEN!" to also be printed to screen as function a is called by main :

main = putStrLn a

Have I missed a step ?

1 Answers

If you remove the let keyword and add a main function it will work:

a = lucky 7

main = putStrLn a

lets are expressions that should be used in body of functions. An Haskell program requires an entry point, that's what the main function is.

Related