Why does this list comprehension fail?

Ganymede

I'm trying to select unique elements from a list like this:

x = [1,1,2,3,4]
s = [e | e <- x, not (e `elem` s)]

It doesn't produce errors, but when I try to read from s it seems like the program hangs. Why?

Plus, what's the right way to do this?

Joshua Taylor

I'm not much of a Haskell-er, but this seems like you've just coded up something sort of like1 Russell's paradox. Aren't you asking for a list s whose elements are those that are in x, but not in s?

s = [e | e <- [1,1,2,3,4], not (e `elem` s)]

So, consider what happens when you try to ask for the first element of s. Well, the first element from e is 1, so 1 will be the first element of s if not (1 `elem` s). Well, is (1 `elem` s)? We can check by iterating over the elements of s and seeing if 1 appeared. Well, let's start with the first element of s

In general suppose that some n is an element of s. Then what must be true? n must be an element of x (easy to check), and also not an element of s. But we supposed that it was an element of s. This is a contradiction. Therefore, no n can be an element of s, so s must be the empty list. Unfortunately, the Haskell compiler isn't doing the proof that we just did, it's trying to programmatically compute the elements of s.

To remove duplicate items from a list, you want the function that Neil Brown recommended in a comment, nub from Data.List:

nub::Eqa => [a] -> [a] Source

O(n^2). The nub function removes duplicate elements from a list. In particular, it keeps only the first occurrence of each element. (The name nub means ‘essence’.) It is a special case of nubBy, which allows the programmer to supply their own equality test.


  1. It's not actually Russell's paradox; Russell's paradox is about a set that contains only those sets that don't contain themselves. That set can't exist, because if it contains itself, then it must not contain itself, and if it does not contain itself, then it must contain itself.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does for-comprehension reverse the input list?

From Dev

Why does for-comprehension reverse the input list?

From Dev

Why does this list comprehension only work on one list?

From Dev

Scala: why does a `for` comprehension on a Map sometimes yield a List?

From Dev

Why does list comprehension give me item not defined error?

From Dev

Why does pdb give NameError for variables referenced in "if" clause of list comprehension?

From Dev

Why is this list comprehension not working?

From Dev

Modify list and dictionary during iteration, why does it fail on dict?

From Dev

Why does this return fail?

From Dev

Why does vectorization fail?

From Dev

Why does this Calendar fail?

From Dev

Why does this macrodef fail?

From Dev

JavaScript "If" - Why does it not fail?

From Dev

why does mmap fail?

From Dev

Why does this fail to compile?

From Dev

why does this script fail?

From Dev

Why does this macrodef fail?

From Dev

JavaScript "If" - Why does it not fail?

From Dev

Why does a for comprehension expand to a `withFilter`

From Dev

List comprehension works but not for loop––why?

From Dev

How does this list comprehension work?

From Dev

Why does list comprehension in my Python Interactive shell append a list of Nones?

From Dev

list comprehension does not return empty list

From Dev

Why does this list comprehension return an Array{Any,1} instead of an Array{Symbol,1}?

From Dev

Why does the map object in Python 3 need to be cast to a list to function with comprehension?

From Dev

Why does this Haskell code fail?

From Dev

Why does split(".") fail? java

From Dev

Why does QueryPerformanceCounter/QueryPerformanceFrequency fail?

From Dev

Why does these sudo commands fail?

Related Related

HotTag

Archive