Find if Duplicates Exist SML NJ

送分小仙女□ 提交于 2019-12-05 14:08:24

As @Marcin said in the comment, an easy and efficient way is to use set for checking duplication. SML/NJ have many set structures available in Utility Library.

Regarding your function, you cannot compare x and myFunc xs since they may not have the same type. And empty list is a list without duplication (myFunc [] should return false).

This works:

fun duplicated [] = false
  | duplicated (x::xs) = (List.exists (fn y => x = y) xs) orelse (duplicated xs)

However, the worst-case time complexity is O(n2) (n is the length of the list) which is quite inefficient.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!