1. Home
  2. /
  3. Haskell
  4. /
  5. Pure Functions

Pure Functions - Haskell Typing CST Test

Loading…

Pure Functions — Haskell Code

Demonstrates Haskell's pure functions, pattern matching, and list operations.

-- Define a custom data type
data Shape = Circle Float | Rectangle Float Float

-- Calculate area using pattern matching
area :: Shape -> Float
area (Circle r) = pi * r * r
area (Rectangle w h) = w * h

-- Higher-order functions
quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) = 
	let smaller = quicksort [a | a <- xs, a <= x]
		bigger = quicksort [a | a <- xs, a > x]
	in smaller ++ [x] ++ bigger

-- List operations
fibonacci :: [Integer]
fibonacci = 0 : 1 : zipWith (+) fibonacci (tail fibonacci)

-- Main function
main :: IO ()
main = do
	let shapes = [Circle 5.0, Rectangle 3.0 4.0, Circle 2.5]
	let areas = map area shapes

	putStrLn $ "Areas: " ++ show areas
	putStrLn $ "First 10 fibonacci: " ++ show (take 10 fibonacci)
	putStrLn $ "Sorted [3,1,4,1,5,9]: " ++ show (quicksort [3,1,4,1,5,9])

Haskell Language Guide

Haskell is a purely functional, statically typed programming language known for immutability, strong type inference, mathematical precision, and high reliability. It is widely used in finance, compilers, research, distributed systems, and correctness-critical software.

Primary Use Cases

  • ▸Pure functional application development
  • ▸Distributed systems
  • ▸Financial trading engines
  • ▸Compilers & language tooling
  • ▸Formal verification
  • ▸Research & algorithm modeling
  • ▸Simulation & high-assurance software

Notable Features

  • ▸Purely functional programming
  • ▸Lazy evaluation model
  • ▸Advanced type system (typeclasses, GADTs, HKTs)
  • ▸Strong type inference
  • ▸Immutability-first design
  • ▸Concise mathematical syntax

Origin & Creator

Developed by a committee of academics in 1990 led by Simon Peyton Jones, Paul Hudak, and Philip Wadler to create a standard pure functional language.

Industrial Note

Haskell excels in domains needing mathematical correctness, high-assurance code, compiler/tooling development, fintech trading systems, distributed ledgers, blockchain research, and formally verifiable system design.

Quick Explain

  • ▸Haskell is purely functional - everything is an expression without mutable state.
  • ▸It features strong static typing powered by an advanced type system.
  • ▸Used for reliable backend systems, research, compilers, finance, and high-assurance software.

Core Features

  • ▸Purity and referential transparency
  • ▸Static strong typing
  • ▸Lazy evaluation
  • ▸Typeclasses for polymorphism
  • ▸Algebraic data types
  • ▸Pattern matching

Learning Path

  • ▸Learn functional thinking
  • ▸Master types & typeclasses
  • ▸Learn monads & effects
  • ▸Work with libraries & frameworks
  • ▸Build real projects

Practical Examples

  • ▸Functional utilities
  • ▸Basic web server
  • ▸Concurrent pipeline
  • ▸Parser combinator
  • ▸Compiler-style transformations

Comparisons

  • ▸Safer and more mathematical than Python
  • ▸More advanced type system than Java
  • ▸Stronger safety guarantees than JavaScript
  • ▸Slower ecosystem growth than Rust

Strengths

  • ▸Extremely safe and reliable
  • ▸Concise, expressive code
  • ▸Powerful type system
  • ▸Great for concurrency
  • ▸Excellent for correctness-critical work

Limitations

  • ▸Steeper learning curve
  • ▸Smaller industry adoption
  • ▸Slower prototyping than Python/JS
  • ▸Harder onboarding for teams
  • ▸Limited mobile ecosystem

When NOT to Use

  • ▸Mobile apps
  • ▸Rapid MVP prototyping
  • ▸Teams without FP experience
  • ▸Large frontend development

Cheat Sheet

  • ▸Function: f x = x + 1
  • ▸List: [1,2,3]
  • ▸Map: map (+1) list
  • ▸Monad: do-notation sequencing
  • ▸Typeclass: class Eq a where ...

FAQ

  • ▸Is Haskell hard?
  • ▸It has a learning curve but becomes extremely powerful.
  • ▸Is Haskell good for production?
  • ▸Yes-used in fintech, compilers, and correctness-critical systems.
  • ▸Is Haskell fast?
  • ▸With optimization, Haskell can be very fast, comparable to C-like languages.
  • ▸Do companies use Haskell?
  • ▸Yes, especially in finance, research, compilers, and blockchain.

30-Day Skill Plan

  • ▸Week 1: Pure functions & types
  • ▸Week 2: Monads & typeclasses
  • ▸Week 3: I/O & concurrency
  • ▸Week 4: Real-world backend

Final Summary

  • ▸Haskell is a purely functional language built for reliability and mathematical correctness.
  • ▸It excels in high-assurance systems, compilers, research, and fintech.
  • ▸Its type system, purity, and laziness make it uniquely powerful.
  • ▸Though harder to learn, it rewards developers with unmatched safety and expressiveness.

Project Structure

  • ▸src/ modules
  • ▸package.yaml or cabal file
  • ▸stack.yaml
  • ▸tests/ folder
  • ▸Main.hs entry point

Monetization

  • ▸Fintech engineering
  • ▸Compiler/PL engineering
  • ▸Backend development
  • ▸High-assurance consulting

Productivity Tips

  • ▸Start in ghci REPL
  • ▸Use pure functions first
  • ▸Add types early
  • ▸Profile laziness

Basic Concepts

  • ▸Immutability
  • ▸Expressions over statements
  • ▸Functions and purity
  • ▸Type system & typeclasses
  • ▸Pattern matching
  • ▸Monads & functors

Official Docs

  • ▸Haskell Report
  • ▸GHC User Guide
  • ▸Haskell Wiki

More Haskell Typing Exercises

Haskell Factorial and RecursionHaskell Map and FilterHaskell Maybe TypeHaskell Zip and List ComprehensionHaskell Higher-Order FunctionsHaskell Pattern Matching on TuplesHaskell Recursion with GuardsHaskell Infinite ListsHaskell Function Composition

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher