我不知道为什么pick1中的sub1函数有类型不匹配的问题,但是pick0没有
(define-predicate one? One)
(: pick1 (-> Positive-Integer (Listof Any) Any))
(define pick1
(?(n lat)
(cond [(one? n) (car lat)]
[else (pick1 (sub1 n)
(cdr lat))])))
Run Code Online (Sandbox Code Playgroud)
我已经尝试过这种解决方法,但是我认为这不是解决此问题的正确方法。
(: pick1-workaround (-> Positive-Integer (Listof Any) Any))
(define pick1-workaround
(?(n lat)
(cond [(one? n) (car lat)]
[else (pick1-workaround (cast (sub1 n) Positive-Integer)
(cdr lat))])))
Run Code Online (Sandbox Code Playgroud)
pick0没有这个问题。
;;(: pick0 (-> Natural (Listof Any) Any))
(define pick0
(?(n lat)
(cond [(zero? n) (car lat)]
[else (pick0 (sub1 n)
(cdr lat))])))
Run Code Online (Sandbox Code Playgroud)