On this page:
3.2.1 Maybe类型定义
Maybe/  c
Just
Nothing
nothing
3.2.2 操作
maybe?
->maybe
maybe-map
maybe-then
maybe-filter
maybe-replace
maybe-and
maybe-or
maybe-alt
maybe-else
maybe->
maybe-unwrap
maybe-catch
8.6

3.2 Maybe

Nil的安全类型,在类型上避免空指针错误。

3.2.1 Maybe类型定义

procedure

(Maybe/c a)  contract?

  a : any/c
“泛型”maybe容器,检测Just是否为a。

procedure

(Just a)  (Maybe/c any/c)

  a : any/c
(Nothing)  (Maybe/c any/c)
Maybe构造器。 同时可用于match

value

nothing : (Maybe/c any/c)

已确定的Maybe的值。无须再调用生成。

3.2.2 操作

procedure

(maybe? x)  boolean?

  x : any/c
检测是否Maybe。

procedure

(->maybe x)  (Maybe/c x)

  x : any/c
将任意一个值转化成Maybe。Racket不像其他语言有个特殊的Nil,它用#f表示“空”, 因此该函数仅会将#f转化为nothing,其他一切都为Just

这引来一个问题,那么我们该如何得到Just #f呢?我的答案是直接调用(Just #f)

Examples:
> (->maybe 1)

(Just 1)

> (->maybe 0)

(Just 0)

> (->maybe #f)

(Nothing)

> (Just #f)

(Just #f)

> (->maybe #t)

(Just #t)

procedure

(maybe-map f a)  (Maybe/c any/c)

  f : (-> any/c any/c)
  a : (Maybe/c any/c)
Functor映射。

Examples:
> (maybe-map add1 (Just 1))

(Just 2)

> (maybe-map add1 nothing)

(Nothing)

procedure

(maybe-then f a)  (Maybe/c any/c)

  f : (-> any/c (Maybe/c any/c))
  a : (Maybe/c any/c)
Monad的binding。

Examples:
> (define (f x)
    (Just (add1 x)))
> (maybe-then f (Just 1))

(Just 2)

> (maybe-then f nothing)

(Nothing)

procedure

(maybe-filter f ma)  (Maybe/c a)

  f : (-> a boolean?)
  ma : (Maybe/c a)
类似于filterf返回#f,整个结果就成了nothing

Examples:
> (maybe-filter even? (Just 2))

(Just 2)

> (define (gt xs)
          (> (length xs) 5))
> (maybe-filter gt (Just (list 1 2 3)))

(Nothing)

> (maybe-filter gt (Just (list 1 2 3 4 5 6)))

(Just '(1 2 3 4 5 6))

procedure

(maybe-replace x ma)  (Maybe/c)

  x : any/c
  ma : (Maybe/c any/c)
替换Just的值,遇到nothing则不变。

Examples:
> (maybe-replace 1 (Just 2))

(Just 1)

> (maybe-replace "hello" (Just 1))

(Just "hello")

> (maybe-replace "hello" nothing)

(Nothing)

procedure

(maybe-and ma mb)  (Maybe/c)

  ma : (Maybe/c any/c)
  mb : (Maybe/c)
类似于andmamb其中之一是nothing,结果就是nothing,反之返回mb

Examples:
> (maybe-and (Just 1) nothing)

(Nothing)

> (maybe-and (Nothing) (Just 2))

(Nothing)

> (maybe-and (Just 1) (Just 2))

(Just 2)

procedure

(maybe-or ma mb)  (Maybe/c any/c)

  ma : (Maybe/c any/c)
  mb : (Maybe/c any/c)
类似于or。见maybe-and

Examples:
> (maybe-or (Just 1) nothing)

(Nothing)

> (maybe-or (Nothing) (Just 2))

(Nothing)

> (maybe-or (Just 1) (Just 2))

(Just 1)

procedure

(maybe-alt ma mb)  (Maybe/c any/c)

  ma : (Maybe/c any/c)
  mb : (Maybe/c any/c)
只有当mamb都为nothing,结果才为nothing

Examples:
> (maybe-alt (Just 1) (Just 2))

(Just 1)

> (maybe-alt (Just 1) (Nothing))

(Just 1)

> (maybe-alt nothing (Just 2))

(Just 2)

procedure

(maybe-else a f ma)  any/c

  a : any/c
  f : (-> any/c any/c)
  ma : (Maybe/c any/c)
根据条件选择对应的回调函数:Just调用fnothing则返回a

Examples:
> (maybe-else 10 add1 (Just 1))

2

> (maybe-else 10 add1 nothing)

10

procedure

(maybe-> x f)  (or/c x any/c)

  x : any/c
  f : (Maybe/c any/c)
maybe,接受一个默认值x,如果fJust,则直接获取Just内容,反之以x代之。

Examples:
> (maybe-> 1 nothing)

1

> (maybe-> 1 (Just 2))

2

procedure

(maybe-unwrap x)  y

  x : (Maybe/c y)
解包Maybe,遇到nothing直接抛异常。

Examples:
> (maybe-unwrap (Just 1))

1

> (maybe-unwrap (Just #f))

#f

> (maybe-unwrap nothing)

maybe-unwrap: 试图解包nothing!

语法

(maybe-catch expr)

 
expr = 任何表达式
自动捕获expr错误,如果expr无任何错误,返回"(Just expr)",反之返回nothing

Examples:
> (maybe-catch (* 1 0))

(Just 0)

> (maybe-catch (/ 1 0))

(Nothing)