Haskell Programming Language Cheatsheet

Basic cheat sheet for Haskell. This cheatsheet covers some fundamental concepts and syntax in Haskell.

1. Basic Syntax

1.1 Declarations

-- Comments start with --
variableName = value

1.2 Functions

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

1.3 Lists

list = [1, 2, 3, 4]

1.4 Tuples

tuple = (1, "Hello")

2. Types and Typeclasses

2.1 Type Declarations

-- Explicit type declaration
add :: Int -> Int -> Int

2.2 Common Types

  • Int, Integer, Float, Double, Char, Bool

2.3 Typeclasses

class Eq a where
  (==) :: a -> a -> Bool
  (/=) :: a -> a -> Bool

3. Pattern Matching

factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n - 1)

4. Lists and List Comprehensions

4.1 Basic List Operations

-- Concatenation
combinedList = [1, 2, 3] ++ [4, 5, 6]

-- Get element by index
elementAtIndex = combinedList !! 2

-- List comparison
isGreaterThan = [1, 2, 3] > [2, 3, 4]

4.2 List Comprehensions

squares = [x^2 | x <- [1..5]]

5. Higher-Order Functions

5.1 Map

squareAll :: [Int] -> [Int]
squareAll list = map (\x -> x^2) list

5.2 Filter

evenNumbers :: [Int] -> [Int]
evenNumbers list = filter (\x -> x `mod` 2 == 0) list

6. Lambdas

multiplyByTwo = \x -> x * 2

7. Monads

7.1 Maybe Monad

safeDivide :: Float -> Float -> Maybe Float
safeDivide _ 0 = Nothing
safeDivide x y = Just (x / y)

7.2 IO Monad

main :: IO ()
main = do
  putStrLn "Hello, World!"

8. Modules

8.1 Importing Modules

import Data.List
import qualified Data.Map as Map

8.2 Creating Modules

module MyModule (function1, function2) where

9. Recursion

factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n - 1)

10. Type Synonyms and Newtypes

10.1 Type Synonyms

type Name = String
type Age = Int

10.2 Newtypes

newtype Meter = Meter Double

Remember to refer to the Haskell documentation and other resources for more in-depth explanations and advanced features. This cheat sheet is meant as a quick reference guide for some fundamental concepts in Haskell.

1.What is Haskell known for in the programming world?

Haskell is a purely functional programming language recognized for its strong type system, lazy evaluation, and expressive syntax.

2.How does Haskell handle side effects in functional programming?

Haskell minimizes side effects through its pure functional paradigm, emphasizing immutability and avoiding state changes.

3.What are the key features that distinguish Haskell from other programming languages?

Haskell stands out with features like lazy evaluation, type inference, pattern matching, and a powerful static type system.

4.Is Haskell suitable for beginners in programming?

While Haskell’s functional paradigm may pose a learning curve, its clean syntax and mathematical foundation make it a valuable language for enhancing programming skills.

5.How does Haskell contribute to software development and industry applications?

Haskell is often used in industries requiring high reliability, such as finance and aerospace, due to its strong type system and mathematical foundations, contributing to robust and maintainable software.