Haskell:
Organizing code into functions
How to:
Here’s how you can write and use functions in Haskell:
-- Defining a simple function to add two numbers
addNumbers :: Int -> Int -> Int
addNumbers x y = x + y
-- Using the function
main = print (addNumbers 3 5)
Output:
8
You can also create higher-order functions:
-- Takes a function and applies it twice to something
applyTwice :: (a -> a) -> a -> a
applyTwice f x = f (f x)
-- Using applyTwice with an anonymous function
main = print (applyTwice (*2) 5)
Output:
20
Deep Dive
Haskell, a purely functional language, treats functions as first-class citizens. Historically, this is rooted in lambda calculus, a foundational framework in computation. Unlike imperative languages where functions are a sequence of instructions, in Haskell, functions are expressions that describe relationships between data.
There are alternatives to writing raw functions for reuse. Consider using typeclasses for polymorphism or leveraging modules to group related functions. Haskell’s lazy evaluation also impacts function implementation—functions won’t evaluate until their results are needed, potentially affecting performance considerations.
See Also
- Official Haskell Documentation: https://www.haskell.org/documentation/
- “Learn You a Haskell for Great Good!” by Miran Lipovača, a beginner-friendly book: http://learnyouahaskell.com/
- “Real World Haskell” by Bryan O’Sullivan, Don Stewart, and John Goerzen: http://book.realworldhaskell.org/