球拍中的要求/打字合同失败

时间:2015-06-02 09:31:02

标签: scheme racket typed-racket

我已经尝试过查看文档,但我无法找到一个可以用于我的案例的示例。我需要从srfi / 1导入break

这是我的尝试。该示例适用于#lang球拍。

#lang typed/racket

(require/typed (only-in srfi/1 break)
               ;[break (All (T) (-> (-> T Boolean) (Listof T) (Listof T)))])
               ;[break (All (T) (-> (-> T Boolean) (Values (Listof T) (Listof T))))])
               [break (All (T) (-> (-> String (U False (Listof T))) (Listof T) (Listof T)))])

(define entity-types '("3DFACE"  "3DSOLID"  "ACAD_PROXY_ENTITY" "ARC" "ARCALIGNEDTEXT"  "ATTDEF"  "ATTRIB"  "BODY"  "CIRCLE" "DIMENSION" "ELLIPSE"  "HATCH" "IMAGE"  "INSERT"  "LEADER"  "LINE" "LWPOLYLINE" "MLINE"  "MTEXT"  "OLEFRAME"  "OLE2FRAME"  "POINT" "POLYLINE" "RAY"  "REGION"  "RTEXT"  "SEQEND"  "SHAPE"  "SOLID" "SPLINE" "TEXT"  "TOLERANCE"  "TRACE"  "VERTEX"  "VIEWPORT" "WIPEOUT" "XLINE"))
(define lst '("ENTITIES" "0" "LINE" "5" "BA1C" "330" "1F" "100" "AcDbEntity" "8" "0" "370" "0" "100" "AcDbLine" "10" "253.2926653556182" "20" "1478.621431186484" "30" "0.0" "11" "253.2876653556182" "21" "1478.621431186484" "31" "0.0" "0" "LINE" "5" "BA1D" "330" "1F" "100" "AcDbEntity" "8" "0" "370" "0" "100" "AcDbLine" "10" "253.2876653556182" "20" "1478.621431186484" "30" "0.0" "11" "253.2876653556182" "21" "1476.655431186484" "31" "0.0" "0" "LINE" "5" "BA1E" "330" "1F" "100" "AcDbEntity" "8" "0" "370" "0" "100" "AcDbLine" "10" "253.2926653556182" "20" "1476.655431186484" "30" "0.0" "11" "253.2876653556182" "21" "1476.655431186484" "31" "0.0" "0" "LINE" "5" "BA1F" "330" "1F" "100" "AcDbEntity" "8" "0" "370" "0" "100" "AcDbLine" "10" "253.2926653556182" "20" "1476.655431186484" "30" "0.0" "11"))

(break (lambda ([x : String]) (member x entity-types)) lst)

2 个答案:

答案 0 :(得分:2)

这对我有用:

(require/typed (only-in srfi/1 break)
               [break (All (T) (-> (-> T (U False (Listof T))) (Listof T) 
                                   (values (Listof T) (Listof T))))])

请注意member返回#f或第一个元素是搜索元素的列表。因此,示例(lambda ([x : String]) (member x entity-types))中的谓词的类型为(-> T (U False (Listof T)))

最后需要values来表明break返回多个值。

答案 1 :(得分:2)

这是我将分配给break的类型:

(require/typed
 (only-in srfi/1 break)
 [break (All [T] (-> (T -> Any) (Listof T) 
                     (Values (Listof T) (Listof T))))])

谓词函数可以返回任何内容。如果它返回#f,它将被视为假,并且任何其他结果将被视为真实。此外,正如soegaard所提到的,需要Values类型构造函数来指示break返回多个值。

但是,Racket实际上包含一个与break非常相似的功能,但它实际上等同于span。该函数本身称为splitf-at,并且就该函数而言,使用break非常容易实现srfi/1

(: break (All [T] (-> (T -> Any) (Listof T) 
                      (Values (Listof T) (Listof T)))))
(define (break pred lst)
  (splitf-at lst (negate pred)))