简体   繁体   English

简洁的方案R5RS使用多个字段定义结构或类

[英]Concise scheme R5RS define struct or class with multiple fields

I've managed to define a struct with one field, how to define multiple fields in one struct or class? 我设法用一个字段定义一个结构,如何在一个结构或类中定义多个字段?

I'm new to R5RS, I can only come up with problematic code, please see it as pseudo code expressing what I'm trying to do. 我是R5RS的新手,我只能拿出有问题的代码,请看一下它是表示我要执行的操作的伪代码。

(define recipe 
    (let (salt 5)
         (sauce "ketchup")))

or 要么

(define recipe
   '((let salt 5)
     (let sauce "ketchup")))

What's the most concise and common way(s) to do this? 什么是最简洁,最常用的方法?

Most Scheme implementations provide records via SRFI 9 . 大多数计划实施都通过SRFI 9提供记录。 So in your case, you can define a recipe record type like so: 因此,根据您的情况,您可以定义一个recipe记录类型,如下所示:

(define-record-type <recipe>
  (recipe salt sauce)
  recipe?
  (salt recipe-salt)
  (sauce recipe-sauce))

Then you can use it like this: 然后,您可以像这样使用它:

> (define salty-ketchup (recipe 5 "ketchup"))
> (recipe-salt salty-ketchup)
5
> (recipe-sauce salty-ketchup)
"ketchup"

If you're using Racket, there's an even simpler syntax for defining structs. 如果您使用的是Racket,则可以使用更简单的语法来定义结构。

(struct recipe (salt sauce))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM