[Haskell-beginners] Ranges and List Comprehensions in SQL
byorgey at seas.upenn.edu
Fri Sep 3 11:43:13 EDT 2010
On Fri, Sep 03, 2010 at 11:21:27AM -0400, Tom Murphy wrote:
> > I am not sure I understand what you are asking. Do you want something
> > like this?
> > data Range = Range Integer Integer
> > toRanges :: [Integer] -> [Range]
> > toRanges = ...
> > toRanges should not be too hard to write but nothing like that exists
> > as far as I know.
> I don't quite understand the functionality of toRanges. When I say "range,"
> I mean a list containing an enumeration, in the form:
[2..300] is just syntactic sugar for the list
[2,3,4,5,6,7,8,9,......,300]. It isn't actually a real "thing" in
Haskell. That's why I made a new data type to represent it:
data Range = Range Integer Integer
toList (Range x y) = [x..y]
So for example (Range 2 300) represents the list [2..300].
> The record would actually be coming from an SQL database.
> I would like to be able to store a large list in SQL records, without having
> to store every element.
I see. So the question seems more about database design than about
Haskell. Toby has suggested a more relational way to model the data,
but his solution does end up storing every element. But you could do
something more like
CREATE TABLE tv_show (
id INTEGER NOT NULL, -- etc
PRIMARY KEY (id)
CREATE TABLE episode_ranges (
tv_show_id INTEGER NOT NULL,
PRIMARY KEY (tv_show_id, start_episode)
Or something like that (it's been a while since I've used SQL). The
data would then be more like
tv_show (30, "The Simpsons")
episode_ranges (30,1,4), (30,14,14), (30,18,21), (30,23,25)
More information about the Beginners