Dr. Racket中的抽象清单
当前,我有一个函数 item counts ,该旨在计算字符串中的元素数量,并将它们成对订购,如果它发生一次或多次。
(check-expect
(item-counts
(cons "hello"
(cons "hey"
(cons "hello" '())))
equal?)
(cons "hello" 2
(cons "hey" 1) '()))
(check-expect
(item-counts
(cons #true
(cons #false
(cons #true '())))
equal?)
(cons #true 2
(cons #false 1 '())))
(define (item-counts lop ef)
(cond
[(empty? (rest lop))
(first lop)]
[(cons? (rest lop))
(if
(ef (first lop) (item-counts (rest lop) ef))) ... ???
如测试所示,如果此列表的一个元素之一不止一次,我希望在现有列表中添加一个数字。我使用均等?函数来识别元素发生的次数,但是我在如何将数字添加到现有列表中以及在产生新列表时将重复元素凝结。我尝试使用 append 函数,但它以不同的格式创建列表((list(list(list)))。
Currently, I have a function item counts that is meant to count the number of elements in a string and order them into pairs if it occurs once or more.
(check-expect
(item-counts
(cons "hello"
(cons "hey"
(cons "hello" '())))
equal?)
(cons "hello" 2
(cons "hey" 1) '()))
(check-expect
(item-counts
(cons #true
(cons #false
(cons #true '())))
equal?)
(cons #true 2
(cons #false 1 '())))
(define (item-counts lop ef)
(cond
[(empty? (rest lop))
(first lop)]
[(cons? (rest lop))
(if
(ef (first lop) (item-counts (rest lop) ef))) ... ???
As shown by the tests, I wish to add a number to the existing list if one of the elements of this list occurs more than once. I used the equal? function to identify the number of times an element occurs, but I am lost on how to add the number to the existing list as well as condense the repeating elements when producing a new list. I have tried using the append function but it creates a list in a different format ( (list (list) ).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道这是否是自学的,对于课程等。我知道有标准的HTDP方法可以做到这一点,食谱等。但是我不知道这些事情,对不起。我要说的是,那个球拍很容易原型。您可以描述您的问题,不要对细节进行太多思考。我通常喜欢从中间开始。
例如:
在执行过程中的某个时刻,您将有尚未检查的字符串以及到目前为止检查的字符串的关联列表。然后,您的任务是1)如果没有更多的字符串要完成,我们完成了,否则2)获取下一个字符串,将其添加到表格,然后继续,因为我们没有完成。
也许您最终不会使用此过程。但这是您正在做的事情的图片,它表明您需要采取什么样的辅助程序来完成任务。给自己打架的机会,然后使用drracket概述您的问题。当您看到它时,也许它会给您一些想法如何回答问题。 Drracket有助于指出您所有未解决的问题,例如“完成?:未绑定的标识符:完成?”啊,是的,我们怎么知道我们已经完成了,好问题drracket。好吧,你知道,我得到了这个字符串列表,所以...
I don't know if this is self-learning, for a class, etc. I know there are standard HtDP ways to do this, recipes, etc. But I don't know these things, sorry. What I will say is that Racket is quite easy to prototype things. You can just describe your problem and don't think too much about the details. I usually like to start in the middle.
For example:
At some point in the middle of execution you will have strings yet to examine and an association list of the strings examined so far. Then your task is 1) if there are no more strings to get, we're done, otherwise 2) get the next string, add it to the table, and continue because we're not done.
Maybe you don't use this procedure in the end. But it's a picture of what you're doing, and it suggests what sort of helper procedures you will need to finish your quest. Give yourself a fighting chance and just use DrRacket to outline your problem. When you see it, then maybe it gives you some ideas how to answer the questions. DrRacket helpfully points out all your unanswered questions like "done?: unbound identifier in: done?" Ah yes, how do we know we're done, good question DrRacket. Well you see, I was given this list of strings, so...