-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagged-data.rkt
47 lines (38 loc) · 944 Bytes
/
tagged-data.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#lang racket
(provide enumerate-tag
get-index
get-value
get-enum-tag
get-enum-value
tag-as
get-tag
replicate)
; data -> tagged data
(define (enumerate-tag tag xs)
(define (range start stop)
(if (> start stop)
'()
(cons start (range (+ 1 start) stop))))
(define (zip x y) (map cons x y))
(zip
(map (curry cons tag) (range 0 (- (length xs) 1))) xs))
; tagged-data -> Int
(define (get-index enum-item)
(cdr (get-tag enum-item)))
; tagged-data -> Symbol
(define (get-enum-tag enum-item)
(car (get-tag enum-item)))
; tagged-data a -> a
(define get-enum-value cdr)
; symbol -> a -> (symbol, a)
(define (tag-as tag item)
(cons tag item))
; forall a. (symbol, a) -> symbol
(define get-tag car)
; forall a. (symbol, a) -> a
(define get-value cdr)
; Int -> a -> [a]
(define (replicate n a)
(if (= n 0)
'()
(cons a (replicate (- n 1) a))))