简体   繁体   English

在Racket中,如何使用struct而不是仅使用高阶函数或递归来查找列表的长度

[英]In Racket how do i find the length of a list using struct instead of just using only higher-order functions or recursion

I'm pretty bad at understanding data structures in Racket right now so I'm currently working on a question where all I have to do is find the length of the list. 我现在很难理解Racket中的数据结构,因此我目前正在研究一个问题,我所要做的就是找到列表的长度。

I understand there are many ways to find the length of a list using recursion and foldr and map but I'm trying to find the length of a list a specific way right now. 我知道有很多方法可以使用递归,文件夹和映射来查找列表的长度,但是我现在正在尝试以特定的方式查找列表的长度。

Here are the details of the question; 这是问题的细节;

For this question, use the following data definition: 对于此问题,请使用以下数据定义:

(define-struct ls (first rest)) (定义结构ls(第一休息))

;; ;; a Ls is either Ls要么是

;; ;; '(), or '(), 要么

;; ;; (make-ls first rest) where first is an Int and rest is a Ls. (make-ls first rest),其中first是Int,rest是Ls。

Keep in mind that since the structure is named ls, and its fields are named first and rest,you will access these fields using ls-first and ls-rest. 请记住,由于该结构的名称为ls,并且其字段的名称分别为first和rest,因此您将使用ls-first和ls-rest访问这些字段。

Here is the question 这是问题

Length. 长度。 Write a function (ls-length L) that consumes a Ls and returns the number of values in it. 编写一个使用Ls并返回其中值个数的函数(ls长度L)。

For example, (check-expect (ls-length (make-ls 5 (make-ls 7 (make-ls 11 '())))) 3) 例如,(check-expect(ls-length(make-ls 5(make-ls 7(make-ls 11'()))))3)

Here's a common way to find the length 这是找到长度的常用方法

(define (length lst)
  (cond
    [(empty? lst)  0]
    [(cons? lst)   (+ 1 (length (rest lst)))]))

I however want the question solved using 但是我想使用

(define-struct ls (first rest))

This is what I have so far but I know that this code seems very wrong, I feel my base case should be correct though. 到目前为止,这是我所拥有的,但是我知道这段代码似乎非常错误,不过我觉得我的基本情况应该是正确的。

(define (ls-length L)
  (cond
    [(empty? L) 0]
    [(cons? (ls-first (first L))) (+ 1 (length (rest (ls-rest))))]))

You have to remember that you can't use cons? 您必须记住您不能使用cons? , rest and first on the new data structure, only empty? restfirst对新的数据结构,只能empty? , ls-first and ls-rest are allowed. 允许使用ls-firstls-rest The conditions are quite simple: the list is either empty or not empty, we don't really need cons? 条件非常简单:列表为空或不为空,我们真的不需要cons? . Also, be careful when calling the recursion, the procedure is called ls-length , not length : 另外,在调用递归时要小心,该过程称为ls-length ,而不是length

(define (ls-length lst)
  (cond
    [(empty? lst) 0]
    [else (+ 1 (ls-length (ls-rest lst)))]))

Compare the above with the well-known length implementation: 将上面的代码与著名的length实现进行比较:

(define (length lst)
  (cond
    [(empty? lst) 0]
    [else (+ 1 (length (rest lst)))]))

You see what happened? 你看到发生了什么事吗? we only had to replace length with ls-length , and rest with ls-rest . 我们只需要替换lengthls-length ,和restls-rest So simple! 很简单!

暂无
暂无

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

相关问题 Haskell使用高阶函数来计算列表中有多少项 - Haskell using Higher-Order Functions to count how many items there are in a list 在球拍中,如何使用递归求和列表中的交替值 - In Racket how do i sum the alternating values in a list using recursion F#将函数转换为使用高阶函数 - F# Transforming function into using higher-order functions 使用高阶函数和Lambda微积分在Haskell中操作列表 - Manipulating Lists in Haskell using Higher-Order Functions and Lambda Calculus 如何使用递归删除 Racket 中列表中的第一个和最后一个元素 - How do I remove the first and last element in a list in Racket using recursion 在使用递归的球拍中,如果列表“ L”的总和为n但L中的值没有重复,我如何返回#true - In Racket using recursion how do I return #true if a list “L” sums to n but no values in L are duplicated 在球拍中,如何仅使用string-> list或list-> string函数替换字符串中的单词? - In racket how do I replace word in string using string->list or list->string function only? 我如何仅使用 cons 和空列表创建列表? 球拍 - How can i create a list just using cons and empty list? Racket 在没有高阶函数或递归的情况下按第二个元素对元组列表进行排序 - Sort a list of tuples by their second element without higher order functions or recursion 使用高阶函数的OCaml错误过滤器列表 - OCaml error filter list using higher order functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM