Basics
This chapter covers the fundamental concepts of Keel.
Values and Types
Keel is a statically typed language with type inference. Here are the basic types:
-- Numbers
let intVal = 42 -- Int
let floatVal = 3.14 -- Float
let price = 19.99d -- Decimal (exact precision)
-- Strings and Characters
let greeting = "Hello" -- String
let letter = 'a' -- Char
-- Booleans
let yes = True -- Bool
let no = False -- Bool
-- Unit (like void)
let unit = ()
greeting
Try itVariables
Variables are declared with let and are immutable:
let x = 42
let name = "Alice"
let pi = 3.14159
name ++ " loves pi"
Try itTo "update" a value, use variable shadowing (re-declaring with the same name):
let x = 1
let x = x + 1
x
Try itComments
-- This is a line comment
{- This is a
block comment -}
let x = 42 -- inline comment
x
Keel also supports doc comments for tooling (LSP hover, documentation generation):
{-| Increments a number by one. -}
fn inc : Int -> Int
fn inc x = x + 1
Indentation
Keel uses significant indentation to define code blocks, similar to Python or Haskell. Nested code must be indented further than its parent:
fn example: Int -> Int
fn example x =
let y = 1 -- Block must be indented
x + y
example 5
Try itConsistent indentation within a block is required:
let condition = True
if condition then
"result1"
else
"result2" -- Must align with 'result1'
Try itFor inline modules, content must be indented:
module Math exposing (add)
fn add: Int -> Int -> Int
fn add x y =
x + y
Math.add 3 4
Both spaces and tabs are accepted, but be consistent within your codebase. The recommended style is 4 spaces per indentation level.
For a detailed explanation of how indentation affects parsing — especially the difference between multilines parsed as arguments vs separate expressions — see Indentation and Scope.
Functions
Functions are first-class values in Keel:
fn add: Int -> Int -> Int
fn add x y =
x + y
add 2 3 -- Returns 5
Try itOperators
Keel supports standard operators:
-- Arithmetic
let sum = 1 + 2
let diff = 5 - 3
let prod = 4 * 2
let quot = 10 / 3
let intDiv = 10 // 3
let power = 2 ^ 8
-- Comparison
let eq = 1 == 1
let neq = 1 != 2
let lt = 1 < 2
let gt = 2 > 1
-- Logical
let andOp = True && False
let orOp = True || False
let notOp = not True -- Not: False
-- String
let concat = "Hello" ++ " World"
-- List
let consed =
1 :: [2, 3]
let joined =
[1, 2] ++ [3, 4]
concat
Try itNext Steps
Continue to learn about indentation and scope for understanding how multiline code is parsed, or jump to functions for function definitions and usage.