使用map、foldr或foldl函数的SML集合交集

发布于 2024-12-19 12:09:46 字数 859 浏览 1 评论 0原文

我正在为课堂做 SML 编程作业,但遇到了一个问题。问题是: “编写一个使用map、foldr或foldl来计算非空集合列表的交集的ML函数。在这里,您可以假设集合表示为列表。对于这个问题,您可以使用辅助命名函数(例如,isMember)提示:非空集合列表至少包含一个集合。”

这就是我到目前为止所拥有的,任何人都可以为我指明正确的方向吗?我对 SML 还很陌生?

fun member(x,[]) = false
|   member(x,L) =
        if x=hd(L) then true
        else member(x,tl(L));

fun intersect(L1,L2) = if tl(L1) = [] then L1 
else if member(hd(L1),L2) = true then L1
else intersect(tl(L1),L2);

fun combine(L1) = if tl(L1) = [] then hd(L1)
else
    foldr intersect [] L1;

我希望代码执行的操作是首先执行具有列表列表的组合函数。它检查是否只有一个列表(即 tl(L1) = []),如果为真,则仅打印第一个列表。如果它为假,我想调用foldr函数,然后调用intersect函数。理论上,在foldr函数期间,我希望它检查第一个列表和第二个列表,并且只保留相同的值,然后检查下一个列表到那些保留的值,并继续执行此操作,直到检查完每个列表为止。完成后,我希望它打印每个列表中的每个值(即集合的交集)。

我知道我的成员函数可以工作,并且组合函数可以执行其应该执行的操作,我的问题是,交集函数有什么问题,有人可以解释交集应该做什么吗?

我显然不想要直接的答案,这不是我来这里的目的。我需要帮助才能找到正确答案。

I am working on a SML programming assingment for class and stuck on a question. The question is:
"Write an ML function that uses map, foldr, or foldl to compute the intersection of a nonempty list of sets.Here you may assume that sets are denoted as lists. For this problem, you may use auxilary named functions (e.g., isMember). Hint: A nonempty list of sets contains at least one set."

This is what I have so far can anyone point me in the right direction I am fairly new to SML?

fun member(x,[]) = false
|   member(x,L) =
        if x=hd(L) then true
        else member(x,tl(L));

fun intersect(L1,L2) = if tl(L1) = [] then L1 
else if member(hd(L1),L2) = true then L1
else intersect(tl(L1),L2);

fun combine(L1) = if tl(L1) = [] then hd(L1)
else
    foldr intersect [] L1;

What I want the code to do is start off by executing the combine function with a lists of lists. It checks if there is only one list (i.e. tl(L1) = []) and if it is true then just print the first list. If its false I want to call the foldr function that then calls the intersect function. In theory during the foldr function I want it to check the first list and second list and only keep what values are the same, then check the next list to those kept values and keeping doing this until it has checked every list. After that is done I want it to print each value that was in each list (i.e. intersection of sets).

I know my member function works and the combine function does what its supposed to do, my QUESTION is, what is wrong with the intersection function and can someone explain what the intersection should be doing?

I obviously don't want the straight up answer, that's not what I am here for. I need help to get on track for the right answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生路 2024-12-26 12:09:46

您的 membercombine 函数看起来不错,但您需要更仔细地考虑 intersect 函数。对于初学者来说,如果 L1 是空列表,则会引发异常,因为 tl 不对空列表进行操作。想想你需要担心的三种情况:

  • L1 is nil
  • hd(L1) is in L2
  • hd(L1)< /code> 不在 L2

找出每种情况下您需要做什么,然后从那里开始。

Your member and combine functions seem fine, but you need to think through the intersect function more carefully. For starters, if L1 is an empty list, it rases an exception, because tl doesn't operate on empty lists. Think about the three cases you need to worry about:

  • L1 is nil
  • hd(L1) is in L2
  • hd(L1) is not in L2

Figure out what you need to do in each case and go from there.

所有深爱都是秘密 2024-12-26 12:09:46

这是我的工作答案!

fun member(x,[]) = false
|   member(x,L) =
        if x=hd(L) then true
        else member(x,tl(L));       

fun intersect([],[]) = []
| intersect(L1,[]) = []
| intersect(L1,L2) = if member(hd(L2), L1) then hd(L2)::intersect(L1, tl(L2))
    else intersect(L1, tl(L2));

fun combine(L1) = if L1 = [] then []
else if tl(L1) = [] then hd(L1)
else foldr intersect (hd(L1)) (tl(L1));

这是运行的一些测试用例。

combine([[1,2],[1,3],[1,4]]);
val it = [1] : int list
combine([[1,2,3],[1,8,9,3],[1,4,3,8,9]]);
val it = [1,3] : int list
combine([[1,2,3,8,9],[1,8,9,3],[1,4,3,8,9]]);
val it = [1,3,8,9] : int list

Here is my working answer!

fun member(x,[]) = false
|   member(x,L) =
        if x=hd(L) then true
        else member(x,tl(L));       

fun intersect([],[]) = []
| intersect(L1,[]) = []
| intersect(L1,L2) = if member(hd(L2), L1) then hd(L2)::intersect(L1, tl(L2))
    else intersect(L1, tl(L2));

fun combine(L1) = if L1 = [] then []
else if tl(L1) = [] then hd(L1)
else foldr intersect (hd(L1)) (tl(L1));

Here are some test cases ran.

combine([[1,2],[1,3],[1,4]]);
val it = [1] : int list
combine([[1,2,3],[1,8,9,3],[1,4,3,8,9]]);
val it = [1,3] : int list
combine([[1,2,3,8,9],[1,8,9,3],[1,4,3,8,9]]);
val it = [1,3,8,9] : int list
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文