Skip to content

PureScript 速查表

编译为 JavaScript 的强类型函数式语言。

01

快速上手

Hello World

PureScript 使用类 Haskell 语法。

purescript
module Main where

import Effect.Console (log)

main = log "Hello, World!"

函数

类型签名用 :: 和 -> 表示。

purescript
add :: Int -> Int -> Int
add a b = a + b

数据类型

用 data 关键字定义代数数据类型。

purescript
data Maybe a = Nothing | Just a

data Either a b = Left a | Right b
02

核心特性

记录

支持行多态的记录。

purescript
type Person = { name :: String, age :: Int }

p :: Person
p = { name: "Alice", age: 30 }

类型类

类型类实现特设多态。

purescript
class Show a where
  show :: a -> String

instance showInt :: Show Int where
  show n = showIntImpl n

do 记法

用 do 记法编写 monadic 代码。

purescript
main = do
  log "First"
  log "Second"

这篇内容对您有帮助吗?