Skip to content

λ Haskell

概述:Haskell 是一种纯函数式编程语言,以数学家 Haskell Curry 命名。自 1990 年发布以来,Haskell 一直是学术研究和函数式编程的标杆,其类型系统和惰性求值机制深刻影响了现代编程语言的设计。

概述

Haskell 属于 函数式 & 分布式语言 分类。Haskell 的核心哲学是:引用透明(无副作用)、强静态类型、惰性求值。

语言特点

  • 纯函数式:函数没有副作用,相同输入永远产生相同输出
  • 惰性求值:表达式仅在需要时才计算,支持无限数据结构
  • 强大类型系统:Hindley-Milner 类型推断 + 高阶类型 + 类型类
  • Monad:通过 Monad 处理副作用(IO、状态、错误等)
  • 模式匹配:函数定义和数据解构的基础

Hello World

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

基础语法

类型与类型类

haskell
-- 基本类型
age :: Int
age = 25

name :: String
name = "Haskell"

price :: Double
price = 99.99

is_active :: Bool
is_active = True

-- 列表
numbers :: [Int]
numbers = [1, 2, 3, 4, 5]

-- 元组
pair :: (Int, String)
pair = (1, "one")

-- Maybe(处理空值)
result :: Maybe Int
result = Just 42
nothing = Nothing

-- Either(处理错误)
outcome :: Either String Int
outcome = Right 42
outcome = Left "error message"

函数定义

haskell
-- 函数类型签名
add :: Int -> Int -> Int
add a b = a + b

-- 模式匹配
factorial :: Int -> Int
factorial 0 = 1
factorial n = n * factorial (n - 1)

-- 守卫(Guard)
classify :: Int -> String
classify n
    | n >= 18   = "成年人"
    | n >= 12   = "青少年"
    | otherwise  = "儿童"

-- 模式匹配 + 守卫
head' :: [a] -> a
head' []    = error "空列表"
head' (x:_) = x

高阶函数

haskell
-- map
doubleAll :: [Int] -> [Int]
doubleAll xs = map (*2) xs

-- filter
evens :: [Int] -> [Int]
evens xs = filter even xs

-- fold
sum' :: [Int] -> Int
sum' xs = foldl (+) 0 xs

-- 函数组合
compose :: (b -> c) -> (a -> b) -> a -> c
compose f g x = f (g x)

-- . 操作符
process = map show . filter even . take 10

类型类

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

-- Show 类型类
class Show a where
    show :: a -> String

-- 自定义类型类实例
data Color = Red | Green | Blue

instance Eq Color where
    Red == Red = True
    Green == Green = True
    Blue == Blue = True
    _ == _ = False

instance Show Color where
    show Red = "Red"
    show Green = "Green"
    show Blue = "Blue"

自定义数据类型

haskell
-- 代数数据类型(ADT)
data Shape
    = Circle Double          -- 半径
    | Rectangle Double Double -- 宽 高
    | Triangle Double Double Double -- 三边

area :: Shape -> Double
area (Circle r) = pi * r * r
area (Rectangle w h) = w * h
area (Triangle a b c) =  -- 海伦公式
    let s = (a + b + c) / 2
    in sqrt (s * (s - a) * (s - b) * (s - c))

-- Record 语法
data Person = Person
    { personName :: String
    , personAge  :: Int
    }

alice = Person { personName = "Alice", personAge = 30 }

Monad 与 IO

haskell
-- do 语法糖
main :: IO ()
main = do
    putStrLn "What's your name?"
    name <- getLine
    putStrLn ("Hello, " ++ name ++ "!")

-- Maybe Monad
safeDivide :: Int -> Int -> Maybe Int
safeDivide _ 0 = Nothing
safeDivide a b = Just (a `div` b)

-- do 记法
compute :: Int -> Int -> Maybe Int
compute x y = do
    a <- safeDivide 100 x
    b <- safeDivide a y
    return (b + 1)

应用领域

  • 编译器开发:GHC 自身就是用 Haskell 编写的
  • 金融建模:形式化验证的金融系统(如 Standard Chartered)
  • 密码学:加密算法的安全实现
  • 学术研究:编程语言理论和类型系统研究
  • 形式化验证:数学证明和程序验证
  • Web 开发:Servant、Yesod 等 Web 框架

开发环境搭建

  1. 安装 GHCup:curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh
  2. 验证:ghc --version
  3. 交互式 REPL:ghci
  4. 构建工具:Stack 或 Cabal
  5. 创建项目:stack new my-project
  6. 编辑器:VS Code + Haskell 扩展

学习路线

  1. 入门阶段:基础语法 → 类型系统 → 模式匹配 → 递归
  2. 进阶阶段:类型类 → Monad → Functor → Applicative
  3. 实战阶段:IO 编程 → Web 开发 → 解析器组合子
  4. 高级阶段:GADT → Type Families → 自由 Monad → 编译器开发

参考资源


返回首页 | 查看所有语言

编程语言技术文档