Lesson 5 of 16

Defining Functions

Defining Functions

Functions in Haskell are defined at the top level, outside of main:

add :: Int -> Int -> Int
add x y = x + y

main :: IO ()
main = print (add 3 4)   -- 7

The type signature Int -> Int -> Int means: takes two Ints, returns an Int. Type signatures are optional (Haskell infers them) but good practice.

Calling Functions

Function application uses a space — no parentheses needed unless grouping:

add 3 4        -- 7
add (add 1 2) 4  -- 7  (grouped)

Multiple Arguments

Haskell functions are curried: add 3 returns a function that adds 3 to its argument:

addThree = add 3
addThree 10    -- 13

How Tests Work

Some tests extract your function definitions and run them with different inputs. When you see a test fail, it means your function was called with a new argument — make sure your logic is general, not hard-coded!

Your Task

Define a function double that multiplies its argument by 2, then print double 21.

Haskell loading...
Loading...
Click "Run" to execute your code.