Variables in Idris

In Idris, variables are explicitly declared and used by the compiler to check type-correctness of function calls.

module Main

import System.IO

main : IO ()
main = do
    -- Declares a variable
    let a = "initial"
    putStrLn a

    -- You can declare multiple variables at once
    let b = 1
    let c = 2
    putStrLn $ show b ++ " " ++ show c

    -- Idris will infer the type of initialized variables
    let d = True
    putStrLn $ show d

    -- Variables declared without a corresponding
    -- initialization are given a default value. For example,
    -- the default value for an Integer is 0.
    let e : Integer
    e = 0
    putStrLn $ show e

    -- In Idris, all variables are immutable by default.
    -- To create a mutable variable, you need to use the IO monad
    -- or create a custom state monad.
    let f = "apple"
    putStrLn f

In Idris, variables are immutable by default, which is different from Go. The var keyword is not used in Idris; instead, we use let to declare variables.

Idris has strong type inference, so we don’t always need to specify types explicitly. However, we can add type annotations when needed.

Unlike Go, Idris doesn’t have a shorthand syntax like := for declaring and initializing variables. All variable declarations use let.

To run this program, you would typically save it in a file with a .idr extension (e.g., variables.idr), and then use the Idris compiler to compile and run it:

$ idris variables.idr -o variables
$ ./variables
initial
1 2
True
0
apple

In Idris, the concept of zero-values is replaced by default values for types. For example, the default value for Integer is 0, for Bool it’s False, and for String it’s an empty string.

Idris is a purely functional language with dependent types, which makes it quite different from Go in many aspects. The examples here have been adapted to fit Idris’ paradigm while trying to maintain the spirit of the original Go code.