In a Haskell file, type declarations are provided separately from definitions, usually on the line before:
c :: Int -- Type declaration
c = 4 -- Definition
This holds true for local definitions, as well as global ones; you just need to make sure the indentation lines up. So in that case, we have
let c :: Int
c = 4
in c + c
In Haskell, newlines and indentation can be replaced with braces and semicolons, and sometimes the braces can be elided. In GHCi, where entering multiple-line input requires some extra machinery, you'll usually want the semicolon-separated variant; to wit, that'll be
let c :: Int ; c = 4
(The lack of an in
is because GHCi behaves a bit like a do
block; this Stack Overflow question has more information.)
However, it looks from your prompt like you have :set +m
turned on, so you can use the multi-line option too:
Prelude> let c :: Int
Prelude| c = 4
Prelude|
Prelude>
(Also, if you want to add a type annotation afterward, let c = 4 ; c :: Int
works fine; it's just not the best style for a file you're working on.)
Also, an important note: you aren't using "Prelude", you're using GHCi, GHC's interactive Haskell environment. Prelude
is the module that is imported by default in all Haskell programs (it provides the definitions of Bool
, Eq
, (.)
, and so on). GHCi's default prompt contains the list of all modules currently imported, so by default it's Prelude>
; however, if you type import Data.Function
, the prompt will change to Prelude Data.Function>
.