Racket 只接受布尔值的结构
如果一个结构是布尔型的,我如何创建一个只接受一个值的结构Racket 只接受布尔值的结构,racket,Racket,如果一个结构是布尔型的,我如何创建一个只接受一个值的结构 (struct mystruct [val] //only take val if it's a boolean ) 你试过了吗 #lang typed/racket (struct point ([q : Boolean] [x : Real] [y : Real])) 你试过了吗 #lang typed/racket (struct point ([q : Boolean] [x : Real] [y : Real]))
(struct mystruct [val] //only take val if it's a boolean )
你试过了吗
#lang typed/racket
(struct point ([q : Boolean] [x : Real] [y : Real]))
你试过了吗
#lang typed/racket
(struct point ([q : Boolean] [x : Real] [y : Real]))
至少有三种方法:
#:guard
定义带有保护的结构
a) 使用合同的警卫
b) 使用除非
,布尔值?
,引发参数错误
手动定义的保护Boolean
的类型(define-struct/contract mystruct ([val boolean?]))
(struct mystruct [val]
#:guard <guard-procedure>)
正确和不正确地使用它:
> (define xt (mystruct #true))
> (define xf (mystruct #false))
> (mystruct-val xt)
#true
> (mystruct-val xf)
#false
> (mystruct 3)
make-mystruct: contract violation
expected: boolean?
given: 3
in: the 1st argument of
(-> boolean? symbol? any)
contract from: (struct mystruct)
blaming: .../so-54901923.rkt
(assuming the contract is correct)
> (define xt (mystruct #true))
> (define xf (mystruct #false))
> (mystruct-val xt)
#true
> (mystruct-val xf)
#false
> (mystruct 3)
mystruct: contract violation
expected: boolean?
given: 3
in: boolean?
contract from:
.../so-54901923.rkt
blaming: .../so-54901923.rkt
(assuming the contract is correct)
2:带防护装置的结构
您可以使用的#:guard
关键字如下:
(define-struct/contract mystruct ([val boolean?]))
(struct mystruct [val]
#:guard <guard-procedure>)
正确和不正确地使用它:
> (define xt (mystruct #true))
> (define xf (mystruct #false))
> (mystruct-val xt)
#true
> (mystruct-val xf)
#false
> (mystruct 3)
make-mystruct: contract violation
expected: boolean?
given: 3
in: the 1st argument of
(-> boolean? symbol? any)
contract from: (struct mystruct)
blaming: .../so-54901923.rkt
(assuming the contract is correct)
> (define xt (mystruct #true))
> (define xf (mystruct #false))
> (mystruct-val xt)
#true
> (mystruct-val xf)
#false
> (mystruct 3)
mystruct: contract violation
expected: boolean?
given: 3
in: boolean?
contract from:
.../so-54901923.rkt
blaming: .../so-54901923.rkt
(assuming the contract is correct)
如果您无法使用类似的功能,或者您需要的电源/控制比提供的更多,则可以手动定义保护程序。如果结构有n个字段,则guard过程应采用n+1个参数,对应于字段和结构名称
例如,对于您的mystruct
,它应该是一个接受val
字段值和名称的函数:
(lambda (val name)
???)
guard过程应使用n个值返回多值结果,对应于字段
(lambda (val name)
(values val))
在传递值之前,它可以对值进行任意检查:
(lambda (val name)
(unless (boolean? val)
(raise-argument-error name "boolean" val))
(values val))
将其放在:guard
:
(struct mystruct [val]
#:guard (lambda (val name)
(unless (boolean? val)
(raise-argument-error name "boolean" val))
(values val)))
使用它:
> (define xt (mystruct #true))
> (define xf (mystruct #false))
> (mystruct-val xt)
#true
> (mystruct-val xf)
#false
> (mystruct 3)
mystruct: contract violation
expected: boolean
given: 3
> (define xt (mystruct #true))
> (define xf (mystruct #false))
> (mystruct-val xt)
- : Boolean
#true
> (mystruct-val xf)
- : Boolean
#false
> (mystruct 3)
Type Checker: type mismatch
expected: Boolean
given: Positive-Byte in: 3
3:打字球拍
你可以用。其形式对每个字段都有一个类型:
#lang typed/racket
(struct mystruct ([val : Boolean]))
使用它:
> (define xt (mystruct #true))
> (define xf (mystruct #false))
> (mystruct-val xt)
#true
> (mystruct-val xf)
#false
> (mystruct 3)
mystruct: contract violation
expected: boolean
given: 3
> (define xt (mystruct #true))
> (define xf (mystruct #false))
> (mystruct-val xt)
- : Boolean
#true
> (mystruct-val xf)
- : Boolean
#false
> (mystruct 3)
Type Checker: type mismatch
expected: Boolean
given: Positive-Byte in: 3
至少有三种方法:
#:guard
定义带有保护的结构
a) 使用合同的警卫
b) 使用除非
,布尔值?
,引发参数错误
手动定义的保护Boolean
的类型(define-struct/contract mystruct ([val boolean?]))
(struct mystruct [val]
#:guard <guard-procedure>)
正确和不正确地使用它:
> (define xt (mystruct #true))
> (define xf (mystruct #false))
> (mystruct-val xt)
#true
> (mystruct-val xf)
#false
> (mystruct 3)
make-mystruct: contract violation
expected: boolean?
given: 3
in: the 1st argument of
(-> boolean? symbol? any)
contract from: (struct mystruct)
blaming: .../so-54901923.rkt
(assuming the contract is correct)
> (define xt (mystruct #true))
> (define xf (mystruct #false))
> (mystruct-val xt)
#true
> (mystruct-val xf)
#false
> (mystruct 3)
mystruct: contract violation
expected: boolean?
given: 3
in: boolean?
contract from:
.../so-54901923.rkt
blaming: .../so-54901923.rkt
(assuming the contract is correct)
2:带防护装置的结构
您可以使用的#:guard
关键字如下:
(define-struct/contract mystruct ([val boolean?]))
(struct mystruct [val]
#:guard <guard-procedure>)
正确和不正确地使用它:
> (define xt (mystruct #true))
> (define xf (mystruct #false))
> (mystruct-val xt)
#true
> (mystruct-val xf)
#false
> (mystruct 3)
make-mystruct: contract violation
expected: boolean?
given: 3
in: the 1st argument of
(-> boolean? symbol? any)
contract from: (struct mystruct)
blaming: .../so-54901923.rkt
(assuming the contract is correct)
> (define xt (mystruct #true))
> (define xf (mystruct #false))
> (mystruct-val xt)
#true
> (mystruct-val xf)
#false
> (mystruct 3)
mystruct: contract violation
expected: boolean?
given: 3
in: boolean?
contract from:
.../so-54901923.rkt
blaming: .../so-54901923.rkt
(assuming the contract is correct)
如果您无法使用类似的功能,或者您需要的电源/控制比提供的更多,则可以手动定义保护程序。如果结构有n个字段,则guard过程应采用n+1个参数,对应于字段和结构名称
例如,对于您的mystruct
,它应该是一个接受val
字段值和名称的函数:
(lambda (val name)
???)
guard过程应使用n个值返回多值结果,对应于字段
(lambda (val name)
(values val))
在传递值之前,它可以对值进行任意检查:
(lambda (val name)
(unless (boolean? val)
(raise-argument-error name "boolean" val))
(values val))
将其放在:guard
:
(struct mystruct [val]
#:guard (lambda (val name)
(unless (boolean? val)
(raise-argument-error name "boolean" val))
(values val)))
使用它:
> (define xt (mystruct #true))
> (define xf (mystruct #false))
> (mystruct-val xt)
#true
> (mystruct-val xf)
#false
> (mystruct 3)
mystruct: contract violation
expected: boolean
given: 3
> (define xt (mystruct #true))
> (define xf (mystruct #false))
> (mystruct-val xt)
- : Boolean
#true
> (mystruct-val xf)
- : Boolean
#false
> (mystruct 3)
Type Checker: type mismatch
expected: Boolean
given: Positive-Byte in: 3
3:打字球拍
你可以用。其形式对每个字段都有一个类型:
#lang typed/racket
(struct mystruct ([val : Boolean]))
使用它:
> (define xt (mystruct #true))
> (define xf (mystruct #false))
> (mystruct-val xt)
#true
> (mystruct-val xf)
#false
> (mystruct 3)
mystruct: contract violation
expected: boolean
given: 3
> (define xt (mystruct #true))
> (define xf (mystruct #false))
> (mystruct-val xt)
- : Boolean
#true
> (mystruct-val xf)
- : Boolean
#false
> (mystruct 3)
Type Checker: type mismatch
expected: Boolean
given: Positive-Byte in: 3