Typed/Racket - 我如何使这个功能工作,不断收到 TypeChecker 错误

问题描述 投票:0回答:1

(: f (-> Procedure (Pairof Integer Integer) Boolean))
   (define (f comparator pair)
      (comparator (first pair) (second pair)))

在TypedRacket中,如何让这个功能发挥作用?该函数应该像这样工作:

(f = '(1 2)) >>>> false.
(f > '(4 2)) >>>> true.

我收到以下错误:

Type Checker: Polymorphic function first could not be applied to arguments
Type Checker: Polymorphic function second could not be applied to arguments
Type Checker: cannot apply a function with unknown arity

所以可能是函数定义导致了这个错误,但是我该如何解决这个问题?

racket typed-racket
1个回答
2
投票

这是一个适用于您的示例的定义:

(: f (-> (-> Integer Integer Boolean) (Listof Integer) Boolean))
(define (f comparator pair)
  (comparator (first pair) (second pair)))

(f = '(1 2))   ; => #f

(f > '(4 2))   ; => #t

您必须将第一个参数的类型定义为从两个整数到布尔值的函数,并将第二个参数定义为列表(因为您在函数调用中使用了列表)。

这是一个简单的定义,只是为了开始使用类型。您可以更改它以将函数应用于更通用类型的值,例如数字而不是整数、多态函数等。

© www.soinside.com 2019 - 2024. All rights reserved.