2019-04-30 21:42:03 +00:00
|
|
|
#lang typed/syndicate/roles
|
|
|
|
|
|
|
|
(require rackunit/turnstile)
|
|
|
|
|
|
|
|
|
|
|
|
(check-type (for/fold ([x 0])
|
|
|
|
([y (list 1 2 3)])
|
|
|
|
x)
|
|
|
|
: Int
|
|
|
|
⇒ 0)
|
|
|
|
|
|
|
|
(check-type (for/fold ([x 0])
|
|
|
|
([y (list 1 2 3)])
|
|
|
|
y)
|
|
|
|
: Int
|
|
|
|
⇒ 3)
|
|
|
|
|
|
|
|
(define-type-alias Inventory (List (Tuple String Int)))
|
|
|
|
|
|
|
|
(define inventory0 (list (tuple "The Wind in the Willows" 5)
|
|
|
|
(tuple "Catch 22" 2)
|
|
|
|
(tuple "Candide" 33)))
|
|
|
|
(check-type (for/fold ([stock 0])
|
|
|
|
([item inventory0])
|
|
|
|
(select 1 item))
|
|
|
|
: Int
|
|
|
|
⇒ 33)
|
|
|
|
|
|
|
|
(check-type (for/fold ([stock 0])
|
|
|
|
([item inventory0])
|
|
|
|
(if (equal? "Catch 22" (select 0 item))
|
|
|
|
(select 1 item)
|
|
|
|
stock))
|
|
|
|
: Int
|
|
|
|
⇒ 2)
|
|
|
|
|
|
|
|
(define (lookup [title : String]
|
|
|
|
[inv : Inventory] -> Int)
|
|
|
|
(for/fold ([stock 0])
|
|
|
|
([item inv])
|
|
|
|
(if (equal? title (select 0 item))
|
|
|
|
(select 1 item)
|
|
|
|
stock)))
|
|
|
|
|
|
|
|
(check-type lookup : (→fn String Inventory Int))
|
|
|
|
|
|
|
|
(define (zip [xs : (List Int)]
|
|
|
|
[ys : (List Int)])
|
|
|
|
((inst reverse (Tuple Int Int))
|
|
|
|
(for/fold ([acc : (List (Tuple Int Int))
|
|
|
|
(list)])
|
|
|
|
([x xs]
|
|
|
|
[y ys])
|
|
|
|
(cons (tuple x y) acc))))
|
|
|
|
|
|
|
|
(check-type (zip (list 1 2 3) (list 4 5 6))
|
|
|
|
: (List (Tuple Int Int))
|
|
|
|
⇒ (list (tuple 1 4) (tuple 2 5) (tuple 3 6)))
|
|
|
|
|
2019-05-13 15:56:57 +00:00
|
|
|
(define (zip-even [xs : (List Int)]
|
2019-04-30 21:42:03 +00:00
|
|
|
[ys : (List Int)])
|
|
|
|
((inst reverse (Tuple Int Int))
|
|
|
|
(for/fold ([acc : (List (Tuple Int Int))
|
|
|
|
(list)])
|
|
|
|
([x xs]
|
|
|
|
#:when (even? x)
|
|
|
|
[y ys]
|
2019-05-13 15:56:57 +00:00
|
|
|
#:unless (odd? y))
|
2019-04-30 21:42:03 +00:00
|
|
|
(cons (tuple x y) acc))))
|
2019-05-13 15:56:57 +00:00
|
|
|
|
|
|
|
(check-type (zip-even (list 1 2 3) (list 5 6 7))
|
|
|
|
: (List (Tuple Int Int))
|
|
|
|
⇒ (list (tuple 2 6)))
|
2019-05-13 19:35:38 +00:00
|
|
|
|
|
|
|
(check-type (for/list ([x (list 1 2 3 4 5 6)]
|
|
|
|
#:when (even? x))
|
|
|
|
(add1 x))
|
|
|
|
: (List Int)
|
|
|
|
⇒ (list 3 5 7))
|
|
|
|
|
|
|
|
(check-type (for/set ([x (set 1 2 3 4 5 6)]
|
|
|
|
#:when (even? x))
|
|
|
|
(add1 x))
|
|
|
|
: (Set Int)
|
|
|
|
⇒ (set 5 3 7))
|
|
|
|
|
|
|
|
(check-type (for/sum ([x (set 8 7 2)])
|
|
|
|
(* x 2))
|
|
|
|
: Int
|
|
|
|
⇒ 34)
|
2019-05-21 20:56:53 +00:00
|
|
|
|
|
|
|
(check-type (for/first ([x (list 1 2 3 4 5)]
|
|
|
|
#:when (even? x))
|
|
|
|
x)
|
|
|
|
: (Maybe Int)
|
|
|
|
⇒ (some 2))
|
|
|
|
|
|
|
|
(check-type (for/first ([x (list 1 2 3 4 5)]
|
|
|
|
#:when (and (even? x)
|
|
|
|
(< x 2)))
|
|
|
|
x)
|
|
|
|
: (Maybe Int)
|
|
|
|
⇒ none)
|