I’m Brent Yorgey. I live with my beautiful wife Joyia in Philadelphia, where I am a PhD student in the programming languages group at the University of Pennsylvania. My academic/professional interests include functional programming languages (especially Haskell), type systems, category theory, combinatorics, discrete mathematics in general, and mathematics education. Other interests include music (I am a classical and jazz pianist), Go, and ancient Greek. Those who wish to know more can try following the “Personal” links listed below, and performing type inference.
March 18, 2008 at 6:16 am |
Hi
I’ve been reading your site for a while now, and it’s very interesting. Great job! you may know the answer to a question that I have been wondering about recently, but cant find the answer. Is it possible (I know Haskell doesnt seem to provide a list subtraction operator) to express a statement of infinite lists like:
[1..] – [2..]
Whichshould yield “1″ as the answer?
March 18, 2008 at 2:19 pm |
Hi Rory,
Haskell does actually provide a list difference operator, it’s called (\\), and can be found in Data.List. For example, [1,2,3,4] \\ [2,3,5] evaluates to [1,4], and [2,2,3] \\ [2,1] evaluates to [2,3]. However, if you try typing [1..] \\ [2..] at the ghci prompt it will sit there forever trying to compute the first value of the result. (\\) can’t deal with infinite lists, since list difference cannot work for infinite lists in general. There’s no way for \\ to know that the lists you gave it happen to have this special structure.
With that said, it should be quite easy to define a new data type which can store this special structure, and a list difference operator (and other operators) which can take advantage of it. For example, something like
data NewList a = Normal [a] | Infinite a | Range a a
Then you could define
listDiff :: (Eq a, Succ a) => NewList a -> NewList a -> NewList a
such that listDiff (Infinite 1) (Infinite 2) evaluated to (Range 1 1) or something like that.
Let me also take this opportunity to point out that the #haskell IRC channel on freenode.net is a fun place full of helpful people (strange, I know!) and you might enjoy hanging out there and asking any more questions you might have.
April 30, 2008 at 10:58 pm |
[...] I happened to stumble upon a short discussion of this problem on Brent Yorgey’s blog. In response to a reader’s question, Brent sketches out a clever approach that introduces a [...]
May 1, 2008 at 9:31 am |
Brent,
I was surprised to stumble on this post because I’ve been thinking this problem recently. I wrote up my thoughts last night and posted them here:
http://techguyinmidtown.com/2008/04/30/differences-and-intersections-on-infinite-lists/
May 1, 2008 at 4:13 pm |
harfga: Cool, thanks for letting me know! I posted a comment over there with another idea that you might find interesting.