我如何划分这些列表?
输入示例:
((a1 . b) (a1 . c)):
我有一个包含两个元素的列表,这些元素是包含两个元素的列表或对。我想检查第一对/列表的第一个元素是否等于第二对/列表的第一个元素。
输出:如果是这样,我想创建一个包含两个列表的新列表,第一个是列表: 而 (b < c) -> (a1 . b(偶数)) (a1 . b+2(偶数))... 另一个列表是相同的,但奇怪的是
我如何在方案中实现这一点:
输入:
((1 . 1) (1 . 7))
输出:
(((1 . 2) (1 . 4) (1 . 6)) ((1 . 3) (1 . 5) (1 . 7)))
我有一个包含两个元素的列表。每个元素也是一个包含两个元素的列表,两个元素都是整数 >= 0 和 <= 0。 8
我必须创建这个:
input ((a1 . b) (a1 . c))
output: (if (and (= a1 a2) (odd? b))
While < b c
(list (a1 . b+1) (a1 . b+3) (a1 . b+n)...))
(list (a2 . b) (a2 . b+2) (a2 . b+4)...)
我已经做到了这一点,但我找不到失败的地方,你能帮助我吗?...
;;; Verify if absissa0 = absissa1
(define (game-position input)
(if (= (car (car j)) (cdr (cdr j)))
(col1_col2 j)
(error "Not valid"))))
;;; verify if absissa0 is even
(define (col1_col2 gstart)
(if (even? (cdr (car jstart)))
(list (pos-start jstart))
(list (pos-start (list (cons (car (car jstart)) (- (cdr (car jstart)) 1)) (car (cdr jstart))))))
;;; Loop that creates positions of even's and odd's
(define (pos-start j2)
(while ( < (cdr (car j2)) (- (cdr (cdr j2)) 2))
((cons (car (car j2)) (+ (cdr (car j2)) 2)) (pos-start (list (cons (car (car j2)) (+ (cdr (car j2)) 2)) (car (cdr j2)))))
(odd_2 (list (cons (car (car j2)) (+ (cdr (car j2)) 1)) (car (cdr j2)))))
(define (odd_2 j3)
(while ( < (cdr (car j3)) (- (car (cdr j3)) 2))
((j3) (odd_2 (list (cons (car (car j3)) (+ (cdr (car j3)) 2)) (car (cdr j3)))
(value)))
Example input:
((a1 . b) (a1 . c)):
I have one list with two elements, those elements are lists or pairs with two elements. And i want to check if the first element of the first pair/list is equal to the first element of the second pair/list.
output: If so, i want to create a new list with two lists, the first is the list:
while (b < c) -> (a1 . b(even)) (a1 . b+2(even))...
The other list is the same, but with the odd's
How do I implement this in scheme:
INPUT:
((1 . 1) (1 . 7))
OUTPUT:
(((1 . 2) (1 . 4) (1 . 6)) ((1 . 3) (1 . 5) (1 . 7)))
I have one list with two elements. Each element is also a list with two elements, both integers >= 0 and < 8
I have to create this:
input ((a1 . b) (a1 . c))
output: (if (and (= a1 a2) (odd? b))
While < b c
(list (a1 . b+1) (a1 . b+3) (a1 . b+n)...))
(list (a2 . b) (a2 . b+2) (a2 . b+4)...)
I had done this, but i can't find where i'm failing, could you help me?....
;;; Verify if absissa0 = absissa1
(define (game-position input)
(if (= (car (car j)) (cdr (cdr j)))
(col1_col2 j)
(error "Not valid"))))
;;; verify if absissa0 is even
(define (col1_col2 gstart)
(if (even? (cdr (car jstart)))
(list (pos-start jstart))
(list (pos-start (list (cons (car (car jstart)) (- (cdr (car jstart)) 1)) (car (cdr jstart))))))
;;; Loop that creates positions of even's and odd's
(define (pos-start j2)
(while ( < (cdr (car j2)) (- (cdr (cdr j2)) 2))
((cons (car (car j2)) (+ (cdr (car j2)) 2)) (pos-start (list (cons (car (car j2)) (+ (cdr (car j2)) 2)) (car (cdr j2)))))
(odd_2 (list (cons (car (car j2)) (+ (cdr (car j2)) 1)) (car (cdr j2)))))
(define (odd_2 j3)
(while ( < (cdr (car j3)) (- (car (cdr j3)) 2))
((j3) (odd_2 (list (cons (car (car j3)) (+ (cdr (car j3)) 2)) (car (cdr j3)))
(value)))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Dlm,你能用“while cicles”做你在代码中所做的同样的事情吗?
你能检查一下我的代码并稍微改进一下吗?我正在努力提高我的编程技能,这是从我的代码开始的,基本上我想获得一种编程风格
Dlm, can you do the same that you did in your code with "while cicles"?
Could you check my code and improve it a litle? I am trying to improve my programming skills, and it's starts from my code, Basicaly I want to get a programming style
对之前的发帖表示抱歉。这是我的第一篇文章
我以未注册用户的身份发帖。我明明
还没有弄清楚如何格式化文本。
我已经创建了一个帐户(用户 dlm)并且正在创建一个
第二次尝试——开始了。
我已经学习 Racket/Scheme 一段时间了
现在这个网站看起来是一个分享和分享的好地方
向别人学习。
我不是 100% 确定这个问题的规范
我怀疑我的代码是否真的解决了问题
手头有,但我认为它的可读性足以进行修改
根据需要
可读性是我一直在努力的事情之一
并希望得到其他人的反馈/建议。
dlm。
我的 2 美分:
更新:
嗨 gn66,
我不知道我实际上能做多少
游戏本身,但我也许可以给你一些
指针/想法。
改进代码时需要注意的一个主要问题是
查找适用于特定情况的重复代码
并尝试想出概括的方法。起初
当你不这样做时,通用形式可能会更难阅读
看看发生了什么,但一旦你完全理解它
它实际上更容易,不仅阅读而且修改。
查看您的代码,“相邻”过程会跳出
作为可以缩短的东西,所以我将用它作为
一个例子。让我们首先忽略边界
条件并寻找一般模式
操作(例如:您将逻辑放在哪里
条件测试对大小有很大影响
代码)。
这里的问题可以分为两个不同的问题
问题:1)改变线位置 + - 1 和
2) 改变行位置 + - 1. 两者都适用
对不同的组件进行相同的操作
位置。因此,让我们只使用其中之一。
(而不是 while 循环,让我们看看 MAP ,它是
就像“while list notempty”循环)
使用“map”将操作应用于数据列表
非常简单:
如果需要,您可以将其包含在程序的范围内
维护状态信息,例如计数器:
或者传递要在操作中使用的值:
现在彻底改变您的想法并考虑这一点
可以将操作列表映射到
一些数据而不是数据来操作。
现在我们有了开始创建新的工具
“相邻”过程:
这适用于查找不在边界上的位置
但正如那句老话所说“有时更容易做到”
某事然后为此道歉比先询问要好
让我们采取同样的方法,让犯错的人
情况发生然后将其删除。 “过滤器”命令是
只是完成工作的工具。
“filter”命令与map命令类似
它接受一个值列表并将它们传递给一个函数。这
“map”命令返回包含新元素的新列表
对应于消耗的每个元素。过滤器返回
原始值,但仅限于(谓词)
函数“批准”(返回 true for)。
将返回列表 (2 4 6 8)
因此,将其添加到新的“相邻”过程中,我们得到:
至于您询问的“while 循环”:您需要
培养从其中“提取”此类信息的能力
现有的例子。您可以探索不同的方面
尝试删除尽可能多的代码来删除现有代码
并仍然让它为您感兴趣的事情工作
(使用打印语句来了解正在发生的事情
在)。这是一个很好的学习方式。
从我的第一篇文章中删除了创建
偶数/赔率列表。当你尝试跑步时你会发现什么是
错误消息中缺少(依赖项),因此
只需根据需要定义它们:
添加打印语句以获取有关如何执行的机制的信息
它有效:(
定义x 1)
(定义最多 5 个)
(定义 y-start 1)
现在删除您不感兴趣或不需要的部分,
这可能需要一些探索:
现在您应该能够更轻松地了解
递归 while 循环并将其用作简单模板
适用于其他情况。
我希望这会有所帮助,并且希望它不会越界
关于“主观问题”指南——我是新手
这个网站并希望融入其中,因为它看起来很棒
资源。
Sorry for the previous posting. It was my first post
and I posted as an unregistered user. I obviously
haven't figured out how to format text yet.
I've created an account (user dlm) and I'm making a
second attempt -- here goes.
I've been working on learning Racket/Scheme for a while
now and this site looks like a great place to share and
learn from others.
I'm not 100% sure of the spec on this question and
have my doubts that my code actually solves the problem
at hand but I think it's readable enough to be modified
as needed
Readability is one of the things I've been working on
and would appreciate feedback/suggestions from others.
dlm.
My 2 cents :
UPDATE:
Hi gn66,
I don't know how much I can actually do in terms of the
game itself but I might be able to give you some
pointers/ideas.
A major thing to look for in improving code is to to
look for repeating code applied to specific situations
and try to think of ways to generalize. At first the
generalized form can seam harder to read when you don't
see what's going on but once you fully understand it
it's actually easier, not only to read but modify.
Looking at your code the 'adjacent' procedure jumps out
as something that could be shortened so I'll use that as
an example. Let's start by first ignoring the boundary
conditions and look for the generial pattern of
operations (example: where you put the logic for
conditional test can have a big effect on the size of the
code).
The problem here can be partitioned into 2 different
problems: 1) changing line postions + - 1 and
2) changing row positions + - 1. Both applying
the same operations to different components of the
position. So let's just work with one.
(instead of a while loop lets look at MAP which is
like a "while list not empty" loop)
Using 'map' to apply an operation to data list(s)
is pretty straight forward:
If needed you can inclose it inside the scope of a procdure
to maintain state information such as a counter:
Or pass in values to use in the operation:
Now turn your thinking inside out and consider that
it's possible to have it map a list of operations to
some data rather than data to the operation.
Now we have the tools to start creating a new
'adjacent' procedure:
This works find for positions that aren't on the boundary
but just as the old saying goes "it's sometimes easier to do
something and then apologize for it than it is to first ask
permission". Let's take the same approach and let the errant
situations occur then remove them. The 'filter' command is
just the tool for the job.
The 'filter' command is similiar to the map command in that
it takes a list of values and passes them to a function. The
'map' command returns a new list containing new elements
that correpsond to each element consumed. Filter returns
the original values but only the ones that the (predicate)
function "approves of" (returns true for).
will return the list (2 4 6 8)
So adding this to the new 'adjacent' procedure we get:
As for the "while cycles" you asked about: you need to
develop the ability to "extract" information like this from
existing examples. You can explore different aspects of
existing code by trying to remove as much code as you can
and still get it to work for what you are interested in
(using print statements to get a window onto what's going
on). This is a great way to learn.
From my first posting cut out the loop that creates the
evens/odds list. When you try to run you find out what is
missing (the dependencies) from the error messages so
just define them as needed:
Add a print statement to get info on the mechanics of how
it works:
(define x 1)
(define max 5)
(define y-start 1)
Now remove parts you aren't interested in or don't need,
which may take some exploration:
Now you should be able to more easily see the mechanics of a
recursive while loop and use this as a simple template
to apply to other situations.
I hope this helps and I hope it doesn't cross the line
on the "subjective questions" guidelines -- I'm new to
this site and hope to fit in as it looks like a great
resource.
我的计划有点生疏,我已经设法解决你的问题,
它使用递归与 while,但我不习惯方案中的构造:
编辑:它在 guile 和 drscheme 中工作。希望它也适用于 plt 方案。
编辑: 子列表内部工作
首先是参数:
函数体非常简单:
如果当前对的 cdr 小于或等于上限,则返回一个列表
由一对 (a . mn) 和通过调用 sublist 创建的列表组成,其中 mn 参数已更改以反映下一个可能的对。
如果当前对的 cdr 高于上限,则返回 null(空列表)以关闭先前调用子列表所发出的 cons。
I'm a bit rusty in scheme, I've managed to get this solution to your problem,
it use recursion vs while, but I'm not accustomed to that construct in scheme:
edit: It works in guile and drscheme. Hope it will works in plt-scheme too.
edit: sublist inner working
First the parameters:
the body of the function is quite simple:
if the cdr of the current pair is smaller or equal to the upper limit then return a list
composed by a pair (a . mn) and the list created by a call to sublist with the mn parameter changed to reflect the next possible pair.
if the current pair will have a cdr higher than the upper limit then return null (empty list) in order to close the cons issued by the previous invocation of sublist.
我已经学习 Racket/Scheme 一段时间了
现在这个网站看起来是一个分享和分享的好地方
向别人学习。
我不是 100% 确定这个问题的规范
我怀疑我的代码是否真的解决了问题
手头有,但我认为它的可读性足以进行修改
根据需要
可读性是我一直在努力的事情之一
并希望得到其他人的反馈/建议。
dlm。
我的 2 美分:
I've been working on learning Racket/Scheme for a while
now and this site looks like a great place to share and
learn from others.
I'm not 100% sure of the spec on this question and
have my doubts that my code actually solves the problem
at hand but I think it's readable enough to be modified
as needed
Readability is one of the things I've been working on
and would appreciate feedback/suggestions from others.
dlm.
My 2 cents :