Haskell
A purely functional programming language
A purely functional programming language
Haskell was designed in 1990 as a standard pure functional programming language for academic and industrial research. It is named after Haskell Curry, a mathematician and logician, and is based on lambda calculus and mathematical rigor.
Haskell's most distinctive feature is lazy evaluation. Expressions are not computed until their values are actually needed, which makes it possible to define infinite data structures and avoid unnecessary computation. In addition, the Hindley-Milner type inference system lets the compiler infer types automatically even without type declarations.
The monad is a core concept in Haskell, providing a way to safely handle side effects (I/O, state changes, etc.) in a pure functional language. There are the IO monad, the Maybe monad, the Either monad, and more, and do notation lets you write them in an imperative style.
In practice, Haskell is used in finance (precise numerical computation), compiler development (GHC itself is written in Haskell), blockchain (Cardano), and static analysis tools. Facebook's spam filtering system Sigma is also written in Haskell and handles billions of requests per day.
-- 1. 기본 함수 정의와 패턴 매칭
factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n - 1)
-- 가드(Guard)를 사용한 조건 분기
grade :: Int -> String
grade score
| score >= 90 = "A"
| score >= 80 = "B"
| score >= 70 = "C"
| otherwise = "F"
-- 2. 리스트와 고차 함수
numbers :: [Int]
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
-- 짝수만 필터링하고 제곱한 후 합계
sumOfEvenSquares :: Int
sumOfEvenSquares = sum $ map (^2) $ filter even numbers
-- 결과: 220 (4 + 16 + 36 + 64 + 100)
-- 리스트 컴프리헨션
pythagoreanTriples :: [(Int, Int, Int)]
pythagoreanTriples = [(a,b,c) | c <- [1..20],
b <- [1..c],
a <- [1..b],
a^2 + b^2 == c^2]
-- [(3,4,5), (6,8,10), (5,12,13), ...]
-- 3. 지연 평가와 무한 리스트
fibs :: [Integer]
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
-- 처음 10개의 피보나치 수 (무한 리스트에서 가져옴)
firstTenFibs :: [Integer]
firstTenFibs = take 10 fibs
-- [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
-- 4. Maybe 모나드를 이용한 안전한 연산
safeDivide :: Double -> Double -> Maybe Double
safeDivide _ 0 = Nothing
safeDivide x y = Just (x / y)
-- do 표기법으로 모나드 연쇄
calculate :: Double -> Double -> Double -> Maybe Double
calculate a b c = do
result1 <- safeDivide a b
result2 <- safeDivide result1 c
return (result2 * 2)
-- 5. 타입 클래스와 다형성
class Describable a where
describe :: a -> String
data Person = Person { name :: String, age :: Int }
instance Describable Person where
describe p = name p ++ ", " ++ show (age p) ++ "세"
-- 사용 예시
main :: IO ()
main = do
putStrLn $ "5! = " ++ show (factorial 5) -- 120
putStrLn $ "Grade: " ++ grade 85 -- B
print firstTenFibs
print $ describe (Person "홍길동" 25) -- "홍길동, 25세"
"This payment logic needs accurate amount calculation, so I think we should write it in Haskell. If we write it with pure functions, the same input always guarantees the same result, and the type system can catch invalid operations at compile time."
"Monads in Haskell are an abstraction of computations with context. The Maybe monad expresses the possibility of failure, and the IO monad expresses side effects, as types, clearly separating pure functions from impure ones. This greatly improves the predictability and testability of code."
"This recursive function isn't tail-recursive, so there's a risk of stack overflow. Because of lazy evaluation, thunks can pile up, so please switch to strict evaluation using seq or BangPatterns, or use a strict version such as foldl'."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
Because of lazy evaluation, thunks (deferred computations) can pile up in memory and cause space leaks. Use foldl' (the strict version) instead of foldl, and force evaluation with seq or deepseq when necessary.
When you combine multiple monads, stacking ReaderT, StateT, ExceptT, and the like makes the types very complex. Consider the mtl pattern or an effect system, and make active use of type aliases.
Make active use of Haskell's powerful type system. If you define the types first and then implement, the compiler will catch many bugs for you. Follow the "Make illegal states unrepresentable" principle and design your types so that invalid states cannot be represented at all.