I am trying to create a datastructure in Haskell, which allows me to efficiently search in an ordered endless list.
If it was Java, I would do something like this:
class LazySet<T> {
private Iterator<T> source;
private NavigableSet<T> set;
public LazySet(Iterator<T> source){
this.source = source;
this.set = new TreeSet<T>() ;
}
public boolean contains(T elem){
while (this.set.ceiling(elem) == null){
if (this.source.hasNext()){
this.set.add(this.source.next());
}else{
return false;
}
}
return this.set.contains(elem);
}
}
Now that this class explicitly has a state, this state is purely for optimization and does not affect the user of the class. Thus, it can be used in a functional manner.
the Haskell equivalent of this class will consist of state.
Maybe something like this:
type LazySet a = (Set a, [a])
member :: Ord a => LazySet a -> a -> (Bool, LazySet a)
And this will make the user explicitly skip LazySet, which greatly complicates its use.
Is there a way to say haskell: Yes, this thing has a state, but treat it as if it weren’t?
source
share