Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  question on typeclasses and applicatives (Daniel Fischer)
   2.  How does one sort with   Data.Vector.Generic.Mutable? (max smith)
   3. Re:  question on typeclasses and applicatives
      (Brandon S Allbery KF8NH)
   4. Re:  Graham Scan exercise from Chapter 3 RWH      -Spoilers. Don't
      read if you want to do this exercise. (J?rgen Doser)
   5. Re:  Graham Scan exercise from Chapter 3 RWH      -Spoilers. Don't
      read if you want to do this exercise. (Stephen Tetley)
   6. Re:  Ranges and List Comprehensions in SQL (Brent Yorgey)
   7. Re:  Ranges and List Comprehensions in SQL (Toby Thain)


----------------------------------------------------------------------

Message: 1
Date: Fri, 3 Sep 2010 02:58:29 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] question on typeclasses and
        applicatives
To: Alec Benzer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

On Friday 03 September 2010 02:11:22, Alec Benzer wrote:
>
> I guess I would then be concerned with why they didn't allow it in the
> standard (though I guess "well, it seemed to be a good idea at the
> time" answers that).

Well, you should ask why. For halfway sane languages like Haskell or C (C89 
or C99, not the mad pre-ANSI stuff), most things in the standard are there 
for good reasons. But some are there because it wasn't noticed early enough 
that it is in fact a bad idea. And of course lots of really good stuff 
isn't in the standard because at the time, nobody even thought of it (or 
had an idea how to implement it).
For completely insane languages like Perl or C++, one cannot assume good 
reasons of course.

>
> I think I also would want to avoid doing things not in the language
> standard on principle, since my instinct would tell me that if only a
> particular compiler implements, I shouldn't use it because it'll
> produce non-standard code. Though this sort of comes comes from C/C++
> where there are different compilers on different platforms, but I
> guess with haskell people pretty much use ghc everywhere?

Pretty much. For a long time, GHC has been the only usable implementation, 
so real world Haskell is de facto Glasgow Haskell. John Meacham's JHC is 
now close to being generally usable, I think. And UHC, the Utrecht Haskell 
Compiler, sounds like a promising project too.



------------------------------

Message: 2
Date: Thu, 2 Sep 2010 18:00:54 -0700 (PDT)
From: max smith <[email protected]>
Subject: [Haskell-beginners] How does one sort with
        Data.Vector.Generic.Mutable?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi,
I have been trying to use Haskell to sort a long list of data (strings, floats 
etc) that is read from a large file (say a few million lines).  I've been 
trying to use a Data.Vector.Generic.Mutable object and a sort algorithm from 
Data.Vector.Algorithms but I've been unable to get it to work since I haven't 
been able to find or create my own examples.  The examples in the excellent 
Vector tutorial, cover filling a vector with random numbers but due to my 
limited understanding of monads, I can't figure out how to use the IO monad and 
fill a vector from a file.  Does anyone have an example of how to do this.
 
Also, is Data.Vector.Generic.Mutable the best way to read a large amount of 
data from a file and sort it?
Thanks for any help,
Max




      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100902/4a8cad1e/attachment-0001.html

------------------------------

Message: 3
Date: Thu, 02 Sep 2010 21:26:53 -0400
From: Brandon S Allbery KF8NH <[email protected]>
Subject: Re: [Haskell-beginners] question on typeclasses and
        applicatives
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 9/2/10 20:11 , Alec Benzer wrote:
> I guess I would then be concerned with why they didn't allow it in the
> standard (though I guess "well, it seemed to be a good idea at the
> time" answers that).

Keep in mind that type theory has advanced a *lot* since Haskell '98 was
frozen.  Quite a few things that are commonplace in modern GHC were
considered prohibitively difficult or expensive to implement back then;
others were adjudged "too confusing" (which got monad comprehensions removed
and the monomorphism restriction added; the latter is almost universally
considered a mistake).

> I think I also would want to avoid doing things not in the language
> standard on principle, since my instinct would tell me that if only a
> particular compiler implements, I shouldn't use it because it'll
> produce non-standard code. Though this sort of comes comes from C/C++
> where there are different compilers on different platforms, but I
> guess with haskell people pretty much use ghc everywhere?

More to the point, ghc is the language developers' playground, so other
compilers (the few that there are) generally follow ghc's lead.

- -- 
brandon s. allbery     [linux,solaris,freebsd,perl]      [email protected]
system administrator  [openafs,heimdal,too many hats]  [email protected]
electrical and computer engineering, carnegie mellon university      KF8NH
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkyATt0ACgkQIn7hlCsL25UFWACcDYjiefvBZl1tH96XR5iOuWt3
Y9gAnjAYZt1UjfOgthSxG72Norny//ng
=8tc2
-----END PGP SIGNATURE-----


------------------------------

Message: 4
Date: Fri, 03 Sep 2010 12:57:38 +0200
From: J?rgen Doser <[email protected]>
Subject: Re: [Haskell-beginners] Graham Scan exercise from Chapter 3
        RWH     -Spoilers. Don't read if you want to do this exercise.
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

El jue, 02-09-2010 a las 17:21 -0700, Michael Litchard escribió:
> Below is my solution for the Graham Scan Algorithm.
> I tried to limit myself to information in the first three chapters
> while completing this problem.
> Now, I want to remove the explicit recursion and use more idiomatic
> Haskell. I'd like to get some advice on which functions/modules would
> be helpful in this.
> 
> 
> <code>
> data Direction = DStraight
>                | DLeft
>                | DRight
>                  deriving (Eq,Show)
> 
> type PointXY = (Double,Double)
> 
> calcTurn :: PointXY -> PointXY -> PointXY -> Direction
> calcTurn a b c
>         | crossProduct == 0 = DStraight
>         | crossProduct > 0  = DLeft
>         | otherwise         = DRight
>        where crossProduct = ((fst b - fst a) * (snd c - snd a)) -
> ((snd b - snd a) * (fst c - fst a))

Instead of using fst, snd, I usually use pattern matching:

calcTurn (x1,y1) (x2,y2) (x3,y3) = ...
        where crossProduct = (x2-x1)*(y3-y1)-...

> calcDirectionList :: [PointXY] -> [Direction]
> calcDirectionList (x:y:z:zs) = (calcTurn x y z) : (calcDirectionList (y:z:zs))
> calcDirectionList _ = []

It is tempting to use some kind of map or foldr for this. Unfortunately,
there isn't a really nice way. Such a "sliding window" map is
occasionally useful, but there is no pre-defined function for it in the
libraries. One way to avoid the explicit recursion is to first create a
list of all the triples, and then map calcTurn over it:

calcDirectionList points = map (\(x,y,z) -> calcTurn x y z)
                (zip3 points (tail points) (tail (tail points)))

Unless one has seen this idiom before, I don't think this is any clearer
than the explicit recursion. Not having a curry3 doesn't help either.


> sortListByY :: [PointXY] -> [PointXY]
> sortListByY [] = []
> sortListByY [a] = [a]
> sortListByY (a:as) = insert (sortListByY as)
>            where insert [] = [a]
>                  insert (b:bs) | snd a <= snd b = a : b : bs
>                                | otherwise      = b : insert bs

You can use Data.List.sortBy and Data.Ord.comparing for this.

> sortListByCoTangent :: [PointXY] -> [PointXY]
> sortListByCoTangent [] = []
> sortListByCoTangent [a] = [a]
> sortListByCoTangent (a:as) = a : insert (sortListByCoTangent as)
>                  where insert :: [PointXY] -> [PointXY]
>                        insert [b] = [b]
>                        insert (b:c:cs) | (myCoTan a b) >= (myCoTan a
> c) =  b : insert (c:cs)
>                                        | otherwise
>  =  c : insert (b:cs)
>                              where myCoTan :: PointXY -> PointXY -> Double
>                                    myCoTan p1 p2 = (fst p2 - fst p1) /
> (snd p2 - snd p1)

This doesn't look like a straightforward sort, because of the special
handling of the first element. After that, it seems to be a sort where
the comparing function takes the first element as an argument. So you
can again use sortBy and comparing.

Next, I try to avoid nesting where to save on horizontal space. And
again, I would use pattern matching instead of fst and snd.

> createHull :: [PointXY] -> [PointXY]
> createHull (a:b:c:cs) = a : filterPoint (createHull (b:c:cs))
>         where filterPoint :: [PointXY] -> [PointXY]
>               filterPoint (x:y:z:zs)
>                         | calcTurn x y z == (DLeft)     = [x] ++ [y]
> ++ [z] ++ filterPoint (zs)
>                         | otherwise                     = [x] ++ [z]
> ++ filterPoint (zs)
>               filterPoint [x] = a:[x]
>               filterPoint _ = []
> createHull _ = []

You pattern match on b,c, but never use them. why not:

createHull (a:as) = a : filterPoint (createHull as)

and handle the case of not enough points in filterPoint itself.

then:

[x]++[y]++[z]++ ... = [x,y,z]++ ...
a:[x] = [a,x]

Again, there is no really nice way to avoid the explicit recursion here.
Are you sure this is the right way to do it, though? There seem to be a
lot of redundant calls to filterPoint.

        Jürgen



------------------------------

Message: 5
Date: Fri, 3 Sep 2010 13:29:50 +0100
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Graham Scan exercise from Chapter 3
        RWH     -Spoilers. Don't read if you want to do this exercise.
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

On 3 September 2010 11:57, Jürgen Doser <[email protected]> wrote:

> It is tempting to use some kind of map or foldr for this. Unfortunately,
> there isn't a really nice way. Such a "sliding window" map is
> occasionally useful, but there is no pre-defined function for it in the
> libraries.

Though not in Data.List, paramorphism is the standard 'sliding window' fold.


-- paramorphism (generalizes catamorphism i.e. foldr)
--
para :: (a -> ([a], b) -> b) -> b -> [a] -> b
para phi b = step
  where step []     = b
        step (x:xs) = phi x (xs, step xs)


With a paramorphism the 'lookahead' is left-to-right, but the list
itself is processed from the right (as per a right-fold). This can
sometimes make it a little tricky to use.


------------------------------

Message: 6
Date: Fri, 3 Sep 2010 11:43:13 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Ranges and List Comprehensions in SQL
To: Tom Murphy <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

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]

[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
        title VARCHAR(80),
        PRIMARY KEY (id)
  );
  CREATE TABLE episode_ranges (
        tv_show_id INTEGER NOT NULL,
        start_episode INTEGER,
        end_episode INTEGER
        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)

-Brent


  



------------------------------

Message: 7
Date: Fri, 3 Sep 2010 12:09:01 -0400
From: Toby Thain <[email protected]>
Subject: Re: [Haskell-beginners] Ranges and List Comprehensions in SQL
To: Brent Yorgey <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes


On 3-Sep-10, at 11:43 AM, Brent Yorgey wrote:

> On Fri, Sep 03, 2010 at 11:21:27AM -0400, Tom Murphy wrote:
> ...
>> 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
>        title VARCHAR(80),
>        PRIMARY KEY (id)
>  );
>  CREATE TABLE episode_ranges (
>        tv_show_id INTEGER NOT NULL,
>        start_episode INTEGER,
>       end_episode INTEGER,
>        PRIMARY KEY (tv_show_id, start_episode)
>  );

This is a possible alternative, but you would need something like a  
CHECK constraint as well, for overlapping ranges. The primary key  
definition above isn't doing much for integrity.

I see no downside whatsoever to "storing every element". It's what an  
RDBMS does: manage relations.

--Toby

>
> 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)
>
> -Brent
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners



------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 27, Issue 8
****************************************

Reply via email to