需要有关要求以特定方式在列表中添加数字的家庭作业问题的提示
Need a hint on a homework question which asks to add numbers in a list in a particular way
这是一道作业题
The function takes in a list as the parameter, which may contain as many layers as sublists as needed For example, '(a (1 b 3)) or '((a 3 5) (b (3 1) 4)). The output has the same list structure of the input (meaning that sublists are maintained), but the car of each list is the sum of all numbers in the list. And all other non-numeric values are discarded. As an example output, consider '((a 3 5) (b (3 1) 4)), the output should be '(16 (8) (8 (4))). Also, only use basic scheme instructions/operations such as + - * /, car, cdr, cons, append, null?, number?, if/else, cond, etc.. Cannot Use a helper method.
到目前为止,这是我拥有的代码,有时部分完成工作。但是我真的很难弄清楚如何从子列表中获取总和以在最外面列表的汽车的一个位置加起来。
(define partialsums*
(lambda (lis)
(cond
[(null? lis) '(0)]
[(list? (car lis)) (cons (partialsums* (car lis)) (if (not (null? (cdr lis))) (partialsums* (cdr lis)) '()))]
[(number? (car lis)) (cons (+ (car lis) (car (partialsums* (cdr lis)))) '())]
[else (cons (+ 0 (car (partialsums* (cdr lis)))) '())])))
我已经在这上面花了几个小时,但不太明白如何正确解决这个问题,可能是因为这是我第一周使用方案 :(。感谢任何帮助。
此外,我无法使用辅助方法。一切都需要以递归方式在一个函数内完成。 letrec
也是不允许的。
为了让生活更轻松,您应该对数据建模。由于没有类型,我们可以非正式地这样做。
输入的结构是什么?
我们可以像 How to Design Programs. Read the "Intertwined Data" section because our data definition is similar to that of an S-expression 中的 "Data Definitions" 那样建模。
; A NestedElem is one of:
; - Atom
; - NestedList
; An Atom is one of:
; - Number
; - Symbol
; A NestedList is one of
; - '()
; - (cons NestedElem NestedList)
我们可以定义一个 atom?
谓词来帮助我们区分程序中数据种类的子句。
; Any -> Boolean
; is `a` an atom?
(define atom?
(lambda (a)
(or (number? a)
(symbol? a))))
程序的结构应该与数据的结构相匹配。
所以我们在我们的数据上定义了一个"template"。它将每个数据区分并分解为子句。它进一步解构了子句的右侧。
; NestedElem -> ...
(define nested-elem-template
(lambda (ne)
(cond
[(atom? ne) ...]
[else ...])))
; Atom -> ...
(define atom-template
(lambda (atom)
(cond [(number? atom) ...]
[(symbol? atom) ...])))
; NestedList -> ...
(define nested-list-template
(lambda (nl)
(cond [(null? nl) ...]
[else (... (car nl)... (cdr nl))])))
我们肯定对数据了解更多。 nested-list-template
中的 (car nl)
是 NestedElem 类型。因此,我们可以通过调用处理此类数据的模板来填充一些 ...
。同样,我们可以围绕已知数据类型的表达式进行递归调用。
; NestedElem -> ...
(define nested-elem-template
(lambda (ne)
(cond
[(atom? ne) (atom-template ne)]
[else (nested-list-template ne)])))
; Atom -> ...
(define atom-template
(lambda (atom)
(cond [(number? atom) ...]
[(symbol? atom) ...])))
; NestedList -> ...
(define nested-list-template
(lambda (nl)
(cond [(null? nl) ...]
[else (... (nested-elem-template (car nl))
... (nested-list-template (cdr nl)))])))
现在我们可以 "fill in the blanks"。
我们可以"filter"、"map"和"fold"覆盖这个数据结构。所有这些都可以使用模板作为脚手架来定义。
注意 1:您的 HW 要求您执行多项任务:
- 删除符号
- 总结数字
cons
每个列表的总和
不要试图在一个函数中完成所有事情。 委托给多个助手 functions/traversals。
注2:我没有建模输出类型。它与输入类型相同,只是 Symbol 不再是原子。
这是一道作业题
The function takes in a list as the parameter, which may contain as many layers as sublists as needed For example, '(a (1 b 3)) or '((a 3 5) (b (3 1) 4)). The output has the same list structure of the input (meaning that sublists are maintained), but the car of each list is the sum of all numbers in the list. And all other non-numeric values are discarded. As an example output, consider '((a 3 5) (b (3 1) 4)), the output should be '(16 (8) (8 (4))). Also, only use basic scheme instructions/operations such as + - * /, car, cdr, cons, append, null?, number?, if/else, cond, etc.. Cannot Use a helper method.
到目前为止,这是我拥有的代码,有时部分完成工作。但是我真的很难弄清楚如何从子列表中获取总和以在最外面列表的汽车的一个位置加起来。
(define partialsums*
(lambda (lis)
(cond
[(null? lis) '(0)]
[(list? (car lis)) (cons (partialsums* (car lis)) (if (not (null? (cdr lis))) (partialsums* (cdr lis)) '()))]
[(number? (car lis)) (cons (+ (car lis) (car (partialsums* (cdr lis)))) '())]
[else (cons (+ 0 (car (partialsums* (cdr lis)))) '())])))
我已经在这上面花了几个小时,但不太明白如何正确解决这个问题,可能是因为这是我第一周使用方案 :(。感谢任何帮助。
此外,我无法使用辅助方法。一切都需要以递归方式在一个函数内完成。 letrec
也是不允许的。
为了让生活更轻松,您应该对数据建模。由于没有类型,我们可以非正式地这样做。
输入的结构是什么?
我们可以像 How to Design Programs. Read the "Intertwined Data" section because our data definition is similar to that of an S-expression 中的 "Data Definitions" 那样建模。
; A NestedElem is one of:
; - Atom
; - NestedList
; An Atom is one of:
; - Number
; - Symbol
; A NestedList is one of
; - '()
; - (cons NestedElem NestedList)
我们可以定义一个 atom?
谓词来帮助我们区分程序中数据种类的子句。
; Any -> Boolean
; is `a` an atom?
(define atom?
(lambda (a)
(or (number? a)
(symbol? a))))
程序的结构应该与数据的结构相匹配。
所以我们在我们的数据上定义了一个"template"。它将每个数据区分并分解为子句。它进一步解构了子句的右侧。
; NestedElem -> ...
(define nested-elem-template
(lambda (ne)
(cond
[(atom? ne) ...]
[else ...])))
; Atom -> ...
(define atom-template
(lambda (atom)
(cond [(number? atom) ...]
[(symbol? atom) ...])))
; NestedList -> ...
(define nested-list-template
(lambda (nl)
(cond [(null? nl) ...]
[else (... (car nl)... (cdr nl))])))
我们肯定对数据了解更多。 nested-list-template
中的 (car nl)
是 NestedElem 类型。因此,我们可以通过调用处理此类数据的模板来填充一些 ...
。同样,我们可以围绕已知数据类型的表达式进行递归调用。
; NestedElem -> ...
(define nested-elem-template
(lambda (ne)
(cond
[(atom? ne) (atom-template ne)]
[else (nested-list-template ne)])))
; Atom -> ...
(define atom-template
(lambda (atom)
(cond [(number? atom) ...]
[(symbol? atom) ...])))
; NestedList -> ...
(define nested-list-template
(lambda (nl)
(cond [(null? nl) ...]
[else (... (nested-elem-template (car nl))
... (nested-list-template (cdr nl)))])))
现在我们可以 "fill in the blanks"。
我们可以"filter"、"map"和"fold"覆盖这个数据结构。所有这些都可以使用模板作为脚手架来定义。
注意 1:您的 HW 要求您执行多项任务:
- 删除符号
- 总结数字
cons
每个列表的总和
不要试图在一个函数中完成所有事情。 委托给多个助手 functions/traversals。
注2:我没有建模输出类型。它与输入类型相同,只是 Symbol 不再是原子。