[Book Note] PFDS: Lists
signature STACK
sig
  type a Stack

  val empty   : a Stack
  val isEmpty : a Stack -> bool

  val cons    : a * a Stack -> a Stack
  val head    : a Stack -> a
  val tail    : a Stack -> a Stack
end
structure List : STACK =
struct
  type a Stack = a list

  val empty = []
  fun isEmpty s = null s

  fun cons (x, s) = x :: s
  fun head s = hd s
  fun tail s = tl s
end
structure CustomStack : STACK = 
struct
  datatype a Stack = NIL
                   | CONS of a * a Stack

  val empty = NIL
  fun isEmpty NIL = true
    | isEmpty _ = false

  fun cons (x, s) = CONS (x, s)
  fun head NIL = raise EMPTY
    | head (CONS (x, s)) = x
  fun tail NIL = raise EMPTY
    | tail (CONS (x, s)) = s
end
fun [] ++ ys = ys
  | (x :: xs) ++ ys = x :: (xs ++ ys)

fun update ([], i, y) = raise SUBSCRIPT
  | update (x :: xs, 0, y) = y :: xs
  | update (x :: xs, i, y) = x :: update(xs, i - 1, y)
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sdq11111/article/details/51546109
想对作者说点什么? 我来说一句

2014 CFA Level 1 Study Note Book1

2014年09月04日 17.7MB 下载

Gof design pattern book and note

2009年07月20日 8KB 下载

没有更多推荐了,返回首页

不良信息举报

[Book Note] PFDS: Lists

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭