Hello World
main :: IO ()
main = putStrLn "Hello, world!"
Run the file:
runhaskell Main.hs
This prints one line from main, which is the usual entry point for a small Haskell program.
Values
Values are immutable. Type annotations are optional, but useful for clarity.
name :: String
name = "Dan"
count :: Int
count = 1
active :: Bool
active = True
These are immutable bindings. You describe what each value is, not how to update it later.
Functions
greet :: String -> String
greet name = "Hello, " ++ name ++ "!"
double :: Int -> Int
double x = x * 2
This shows two simple pure functions. They take input and return output without changing outside state.
Haskell Add
add :: Num a => a -> a -> a
add param1 param2 = param1 + param2
This just adds the two input numbers with the language’s normal arithmetic and returns the sum.
Haskell Add Border
addBorder :: [String] -> [String]
addBorder picture = border : map (\row -> "*" ++ row ++ "*") picture ++ [border]
where
border = replicate (length (head picture) + 2) '*'
This builds a new grid with a * border around every side. It adds a full top and bottom row, then wraps each existing row from left and right.
Haskell Adjacent Elements Product
adjacentElementsProduct :: [Int] -> Int
adjacentElementsProduct inputArray = maximum (zipWith (*) inputArray (tail inputArray))
This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.
Haskell Almost Magic Square
almostMagicSquare :: [Int] -> [Int]
almostMagicSquare a = concat (go rows rowSums colSums 0 0)
where
chunk3 [] = []
chunk3 xs = take 3 xs : chunk3 (drop 3 xs)
updateAt :: Int -> (b -> b) -> [b] -> [b]
updateAt idx f xs = [if i == idx then f x else x | (i, x) <- zip [0 ..] xs]
rows = chunk3 a
rowSums = map sum rows
colSums = [sum [rows !! r !! c | r <- [0 .. 2]] | c <- [0 .. 2]]
maxSum = maximum (rowSums ++ colSums)
go grid rs cs i j
| i >= 3 || j >= 3 = grid
| otherwise =
let diff = min (maxSum - rs !! i) (maxSum - cs !! j)
grid' = updateAt i (updateAt j (+ diff)) grid
rs' = updateAt i (+ diff) rs
cs' = updateAt j (+ diff) cs
i' = if rs' !! i == maxSum then i + 1 else i
j' = if cs' !! j == maxSum then j + 1 else j
in go grid' rs' cs' i' j'
This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.
Haskell Are Equally Strong
areEquallyStrong :: Int -> Int -> Int -> Int -> Bool
areEquallyStrong yourLeft yourRight friendsLeft friendsRight =
max yourRight yourLeft == max friendsLeft friendsRight
&& min yourLeft yourRight == min friendsRight friendsLeft
This compares each person’s strongest and weakest arm. If both pairs match, the result is true.
Haskell Array Change
arrayChange :: [Int] -> Int
arrayChange [] = 0
arrayChange (x : xs) = snd (foldl step (x, 0) xs)
where
step (prev, total) cur
| prev >= cur = (prev + 1, total + (prev - cur + 1))
| otherwise = (cur, total)
This moves left to right and bumps values only when needed so the array becomes strictly increasing.
Haskell Array Maximal Adjacement Difference
arrayMaximalAdjacentDifference :: [Int] -> Int
arrayMaximalAdjacentDifference a
| length a < 3 = 0
| otherwise = maximum (zipWith3 middle a (tail a) (drop 2 a))
where
middle p c n = max (abs (c - p)) (abs (c - n))
This checks the gap between each pair of neighbors and returns the largest difference.
Haskell Binary Gap
import Data.Char (intToDigit)
import Data.List (dropWhileEnd)
import Numeric (showIntAtBase)
binaryGap :: Int -> Int
binaryGap n = maximum (0 : map length (words spaced))
where
bin = showIntAtBase 2 intToDigit n ""
trimmed = dropWhileEnd (== '0') (dropWhile (== '0') bin)
spaced = map (\c -> if c == '1' then ' ' else c) trimmed
This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.
Haskell Bracket
bracket :: String -> Int
bracket s = go s []
where
go [] stack = if null stack then 1 else 0
go (c : cs) stack = case c of
')' -> pop '(' cs stack
']' -> pop '[' cs stack
'}' -> pop '{' cs stack
_ -> go cs (c : stack)
pop expected cs (top : rest)
| top == expected = go cs rest
pop _ _ _ = 0
This uses a simple stack approach: open brackets go in, matching closing brackets pop them out.