Function Definition
Browse / Haskell Cheatsheet
Haskell Cheatsheet
A concise reference for Haskell syntax, data types, functions, and common operations. This cheat sheet provides a quick guide to help you write and understand Haskell code efficiently.
Basic Syntax and Types
Syntax Fundamentals
|
|
Example:
|
|
Comments |
|
|
Layout |
Haskell uses indentation to define blocks. Consistent indentation is crucial. |
|
Let Bindings |
|
|
Where Bindings |
|
|
Case Expressions |
|
Basic Data Types
|
Int |
Integer values with a fixed range. |
|
Integer |
Integer values with arbitrary precision. |
|
Float/Double |
Floating-point numbers. |
|
Bool |
Boolean values: |
|
Char |
Single Unicode characters. |
|
String |
List of characters. |
Type Signatures
|
Explicit type signatures are recommended for clarity.
Example:
|
Functions and Control Flow
Function Application
|
Basic Application |
Functions are applied by simply placing arguments after the function name.
Example:
|
|
Parentheses |
Parentheses are used to control precedence.
|
|
Composition |
Function composition using
|
Control Flow
|
If-Then-Else |
Example:
|
|
Guards |
Guards provide an alternative to if-then-else for defining functions.
Example:
|
|
Case Expressions |
Pattern matching with
|
Lambda Expressions
|
Anonymous functions defined using
Example:
|
Data Structures
Lists
|
List Syntax |
Example:
|
|
Cons Operator |
The
Example:
|
|
List Comprehension |
Example:
|
|
Common Functions |
|
Tuples
|
Tuple Syntax |
Example:
|
|
Accessing Elements |
|
Data Type Declarations
Example:
|
Typeclasses and Monads
Typeclasses
|
Defining a Typeclass |
|
|
Instances |
|
|
Common Typeclasses |
|
Monads
|
Monad Typeclass |
|
|
Do Notation |
Syntactic sugar for working with Monads.
|
|
Common Monads |
|
IO Monad
|
Used for performing input/output operations.
|