💾 Archived View for gem.sdf.org › s.kaplan › cheatsheets › programming-languages › haskell.md captured on 2024-05-12 at 15:32:38.

View Raw

More Information

⬅️ Previous capture (2023-09-28)

-=-=-=-=-=-=-

# Haskell Cheatsheet

## Overview of unique features

- Lazy evaluation
- Purely functional programming language
- Strong static typing
- Type inference
- Higher-order functions
- Pattern matching
- Monads

## Variables

-- Declare a variable

x = 42

-- Declare a constant

y :: Int

y = 10

-- Declare a list

myList = [1, 2, 3]

-- Declare a tuple

myTuple = (1, "hello")


## Functions

-- Declare a function

add :: Int -> Int -> Int

add x y = x + y

-- Call a function

result = add 3 4


## Loops

Haskell uses recursion instead of loops.

-- Define a recursive function to iterate over a list

sumList :: [Int] -> Int

sumList [] = 0

sumList (x:xs) = x + sumList xs


## Conditionals

-- Define an if-else statement

max :: Int -> Int -> Int

max x y = if x > y then x else y


## File manipulation

import System.IO

-- Open a file

main = do

handle <- openFile "file.txt" ReadMode

contents <- hGetContents handle

putStr contents

hClose handle


## Resources

- [Haskell documentation](https://www.haskell.org/documentation/)
- [Learn You a Haskell for Great Good!](http://learnyouahaskell.com/)
- [Real World Haskell](http://book.realworldhaskell.org/)