如何在racket中编写返回字符串或#f(false)的声明?

发布于 2025-01-18 20:23:41 字数 186 浏览 1 评论 0原文

我在球拍中有以下功能:(

:search-stack:Symbol KeyStack -> String) (define (search-stack s stack)

该函数在堆栈中搜索特定字符串并返回匹配的符号。如果未找到该字符串,则该函数返回“#f”。如何修改返回类型以允许返回字符串或布尔类型(仅限 false)?

I have the following function in racket:

( : search-stack : Symbol KeyStack -> String)
(define (search-stack s stack)

This function searches the stack for a specific string and returns the matching symbol. If the string is not found, the function returns "#f". How do I modify the return type to allow for returning either a string or boolean type (false only)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光与爱终年不遇 2025-01-25 20:23:41

考虑更简单的函数 match,它只检查一对(可以是
堆栈的一个元素)。使用签名(非正式类型作为注释),这可能是:

#lang racket

(define (match s p) ;; Symbol (Symbol . String) -> "String or #f"
  ;; produce string if s matches, otherwise #f
  (if (eq? s (car p))
      (cdr p)
      #f))

在类型化的racket中,在参数上带有类型注释,这就是:

#lang typed/racket

(define (match [s : Symbol] [p : (Pairof Symbol String)])
  (if (eq? s (car p))
      (cdr p)
      #f))

现在只需在DrRacket交互中输入match即可面积和类型归纳
将显示返回类型:

Welcome to DrRacket, version 8.4 [cs].
Language: typed/racket, with debugging.
> match
- : (-> Symbol (Pairof Symbol String) (U False String))
#<procedure:match>
> 

Consider the simpler function match, which just checks a pair (which could be
one element of a stack). With signature (informal type as a comment) this could be:

#lang racket

(define (match s p) ;; Symbol (Symbol . String) -> "String or #f"
  ;; produce string if s matches, otherwise #f
  (if (eq? s (car p))
      (cdr p)
      #f))

In typed-racket, with type annotations on the arguments, this is:

#lang typed/racket

(define (match [s : Symbol] [p : (Pairof Symbol String)])
  (if (eq? s (car p))
      (cdr p)
      #f))

Now just type match into the DrRacket interactions area, and type induction
will reveal the return type:

Welcome to DrRacket, version 8.4 [cs].
Language: typed/racket, with debugging.
> match
- : (-> Symbol (Pairof Symbol String) (U False String))
#<procedure:match>
> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文