在 Scheme 中创建一个集合的分区 [英] Creating a partition of a set in Scheme

查看:60
本文介绍了在 Scheme 中创建一个集合的分区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对整体计划还很陌生,我在为学校安排作业时遇到了一些问题.所以请不要提供完整的答案,只是寻找一些见解或朝正确方向的推动,这样我就可以自己解决这个问题.

I'm pretty new to scheme overall and I'm having some issues with figuring out an assignment for school. So please no full answers, just looking for a little insight or a nudge in the right direction so I can figure this out on my own.

问题如下:给定一个数字列表,确定是否可以从这些等价和和项目数量的数字组成两个子集.例如,如果给定的集合是 (1 1) 那么我的程序应该返回 #t,否则返回 #f.

The problem is as follows: Given a list of numbers, determine whether two subsets can be made from those numbers of equivalent sum and # of items. So for example, if the given set is (1 1) then my program should return #t, otherwise #f.

这是我到目前为止所写的(即使它目前没有输出)

Here is what I have written so far (even though it gives no output at the moment)

(define l (list '(7 5)))

(define s1 0)
(define s2 0)
(define l1 0)
(define l2 0)

(define two-subsets
  (lambda (l s1 s2 l1 l2)
    (if (null? l)
          (if (and (= s1 s2) (= l1 l2))
              (#t))
          (if (not (and (= s1 s2) (= l1 l2)))
              (#f)))
    (or ((two-subsets(cdr l) (+ s1 1) s2 (+ l1 1) l2) (two-subsets(cdr l) s1 (+s2 1) l1 (+ l2 1))))))

我的函数应该递归地将项目添加到子集 1 (s1, l1) 或子集 2 (s2, l2) 中,直到达到基本情况,确定子集的大小和总和是否相等.我觉得我的逻辑已经存在/接近了,但我不确定如何在方案中正确实施这些想法.

My function should recursively add items to either subset 1 (s1, l1) or subset 2 (s2, l2) until it reaches the base case where it determines whether or not the subsets are of equivalent size and sum. I feel like my logic is there / close, but I'm unsure how to implement these ideas properly in scheme.

EDIT 我应该补充一点,作为作业的一部分,我必须使用递归.我希望 DrRacket 提供更多调试信息,因为当我点击运行时,它不会出错,也没有输出.

EDIT I should add that, as a part of the assignment, I must use recursion. I wish DrRacket gave more debugging info because when I hit run it gives no errors, but also no output.

推荐答案

Issues

您的代码存在一些问题.

Issues

There are some issues in your code.

这将创建一个列表:

(list '(7 5))  ; => ((7 5))

其中的 cdr 始终是一个空列表.

A cdr of that is always an empty list.

(cdr (list '(7 5)))  ; => ()

以这种方式创建单个列表:

A single list is created in this way:

(define l (list 7 5))

或者这样:

(define l '(7 5))

第二个

您在 Scheme 中使用括号进行应用.这:

Second

You use parenthesis in Scheme for application. This:

(#t)

表示执行函数#t".但是 #t 不是函数,它是一个布尔值.并且不能执行布尔值.

means "execute the function #t". But #t is not a function, it is a Boolean value. And Boolean values can not be executed.

你可以直接返回一个布尔值

Your can return a Boolean value directly

#t

或者你可以返回一个返回值的函数

or you can return a function returning the value

(lambda () #t)

但是你不能执行true.

but you can not execute true.

同样的问题.代码如下:

Same problem in or. The following code:

(or ((two-subsets (cdr l) (+ s1 1) s2 (+ l1 1) l2)
     (two-subsets (cdr l) s1 (+s2 1) l1 (+ l2 1))))

表示:two-subsets 必须返回一个函数.第一个 two-subsets 调用返回的函数与第二个 two-subsets 调用返回的函数一起执行.并且将单个结果值传递给 or.这可能不是您想要的.

means: two-subsets must return a function. The function returned by the first two-subsets call gets executed with the function returned by the second two-subsets call. And the single result value gets passed to or. This is probably not what you want.

如果要or两次调用two-subsets的两个返回值,必须去掉两个括号.

If you want to or the two return values of the two calls to two-subsets, you have to remove two parenthesis.

(or (two-subsets (cdr l) (+ s1 1) s2 (+ l1 1) l2)
    (two-subsets (cdr l) s1 (+s2 1) l1 (+ l2 1)))

提示

  • 定义一个与您的结束条件相匹配的函数.该函数接受两个列表参数并检查它们是否具有相同的大小(length)和总和(您可以使用 apply 将列表传递给 +).剧透
  • 编写一个遍历所有可能子集的函数.并为每个组合调用您的匹配函数.迭代是通过 Scheme 中的递归完成的.要么定义一个调用自身的函数,要么使用一个命名的 let.
  • Hints

    • Define a function which matches your end condition. The function takes two list arguments and checks, if they have the same size (length) and sum (you can use apply to pass a list to +). Spoiler
    • Write a function which iterates through all possible subsets. And call your match function with each combination. Iteration is done by recursion in Scheme. Either define a function, which calls itself or use a named let.
    • 这篇关于在 Scheme 中创建一个集合的分区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆