• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: SML – See if two unordered lists are equal (have same values, and the values appear the same amount of times)

Resolved: SML – See if two unordered lists are equal (have same values, and the values appear the same amount of times)

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

Like the title says, I’m trying to see if two unordered lists include the same values and if these values appear the same amount of times in these lists. The assignment given asks not to order the lists. Here’s what I’ve done so far:
  • my logic was to compare recursively if the second list (from now on l2 ) includes the hd (= first element) of the first list ( from now on l1 ) if it does, compare the tl of l1. And as long as I call this isEqual function I would remove the hd of l1 and the first position in which the same value appears in l2. My base case would be that the 2 lists have different lengths then I would know for sure they are not equal. Here’s the code:

Below the output this function should return based on the lists passed:
The error in which I encounter is the following:

Answer:

I think you’re overthinking this problem.
This is good so far, and makes sense.
Now, if neither of the lists are empty, then let’s pattern match that so that we can get to the head and tail of the first list.
Now, if we remove the value x from lst2 and run the same function recursively on xs and what’s left of lst2, we should be able to reduce down to a result.
So let’s write a helper remove function. Your delete is on the right track, but it’s reliant on an index, and indexes aren’t really a natural fit with lists.
The option type is a good choice to represent a case where we try to remove a value that is not in a list. Getting NONE would tell us pretty handily that the two are not equal.
I’ll let you use this to complete isEqual.
Of course, you could also just sort both lists, and then this becomes much more straightforward.

If you have better answer, please add a comment about this, thank you!

design-patterns function list matching sml
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How to compare two text files and and change the sign of the data in powershell?

27/03/2023

Resolved: Java Virtual Machines deleted

27/03/2023

Resolved: PyCharm cannot see my newly compiled .pyc see on import

27/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.