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:  Possible to update Haskell syntax (Steven Ashley)
   2. Re:  Trouble with formatting,     Real World Haskell example
      (Steven Ashley)
   3.  Efficient sieve of erastothenes, for solving project euler
      problem #10? (Malcolm Reynolds)
   4. Re:  Efficient sieve of erastothenes,     for solving project
      euler problem #10? (Christos Chryssochoidis)
   5. Re:  Efficient sieve of erastothenes,     for solving project
      euler problem #10? (David Frey)
   6.  Re: Efficient sieve of erastothenes, for solving project
      euler problem #10? (apfelmus)
   7.  GTK + Reactive (Levi Stephen)
   8. Re:  Trouble with formatting,     Real World Haskell example
      (Kurt Hutchinson)


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

Message: 1
Date: Sun, 23 Nov 2008 18:00:17 +1300
From: "Steven Ashley" <[EMAIL PROTECTED]>
Subject: Re: [Haskell-beginners] Possible to update Haskell syntax
To: [email protected]
Message-ID:
        <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-1

Hi cm,

An ambiguity arises when you have a list containing functions. For
instance consider:

fmap ($3) [id id id id]

Cheers,

Steven


2008/11/23 cm <[EMAIL PROTECTED]>:
> I'm very interested in learning Haskell, but one frustration is that lists
> always require commas.  As an example [1,2,3,4] is a valid expression, but
> [1 2 3 4] is not.
>
> In this ideal world, another example would be that [(fun1 3 4) 7] would be a
> valid expression and be a two-element list equivalent to [fun1 3 4, 7].
>
> If what I am suggesting is syntactically possible and doesn't break more
> advanced features of the language that I (as a beginner) am unaware of, I'm
> interested in patching the open source for my own use (as well as others who
> might want it).
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>


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

Message: 2
Date: Sun, 23 Nov 2008 18:10:24 +1300
From: "Steven Ashley" <[EMAIL PROTECTED]>
Subject: Re: [Haskell-beginners] Trouble with formatting,       Real World
        Haskell example
To: [email protected]
Message-ID:
        <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-1

Hi Robert,

The contents of the do block must be indented at-least as much as
mainWith on the previous line.

For instance:

import System.Environment (getArgs)

interactWith function inputFile outputFile = do
....input <- readFile inputFile
....writeFile outputFile (function input)

main = mainWith myFunction
....where mainWith function = do
....................args <- getArgs
....................case args of
..............................[input, output] -> interactWith function
input output
.............................._ -> putStrLn "Usage: Interact inputFile
outputFile"
....................myFunction = id


The haskell wikibook has some more detailed information of
http://en.wikibooks.org/wiki/Haskell/Indentation

Regards,

--Steven

2008/11/23 Robert Kosara <[EMAIL PROTECTED]>:
> I'm having some trouble getting the InteractWith example from Real World
> Haskell's Chapter 4 to compile. When I use spaces (like below), it says
> "Interact.hs:10:30: Empty 'do' construct." When I use tabs (4 spaces per
> tab), I get "Interact.hs:16:13: parse error on input `='." In either case, I
> don't see the problem. Any help would be greatly appreciated.
> I'm also confused about the formatting of the example in the book (page 72).
> The last line ("myFunction = id") seems to be indented between the where and
> the next line, why is that? Is that simply a layout problem? To me, it seems
> it should be on the same level as the where.
> Below is my program:
> -- Interact.hs, simple filter in Haskell
> import System.Environment (getArgs)
> interactWith function inputFile outputFile = do
>     input <- readFile inputFile
>     writeFile outputFile (function input)
> main = mainWith myFunction
>     where mainWith function = do
>         args <- getArgs
>         case args of
>             [input, output] -> interactWith function input output
>             _ -> putStrLn "Usage: Interact inputFile outputFile"
>         myFunction = id
>
> Regards,
> Robert
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>


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

Message: 3
Date: Sun, 23 Nov 2008 22:28:23 +0000
From: "Malcolm Reynolds" <[EMAIL PROTECTED]>
Subject: [Haskell-beginners] Efficient sieve of erastothenes,   for
        solving project euler problem #10?
To: [email protected]
Message-ID:
        <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-1

Hello all,

I'm attempting to learn Haskell by going through the project euler
problems. Number 10,
http://projecteuler.net/index.php?section=problems&id=10 , involves
summing prime numbers. It's easy in terms of coding something up that
works, but I'm having a lot of trouble getting decent performance.
I've learned a reasonable amount of ML at uni but Haskell is the first
lazy language I've used.. I think the inefficiency is possibly due to
the laziness but I'm not positive.

I'd love if someone could show me how to do this in Haskell somewhere
near as fast as C - at the moment I have a C version which runs in
about a tenth of a second (
http://github.com/malcster/project-euler-solutions--c-/tree/master/10.c
). My haskell attempts are
http://github.com/malcster/project-euler-solutions/tree/master/10better.hs
(using the sieve) and
http://github.com/malcster/project-euler-solutions/tree/master/10.hs
(using possibly an even worse method, but seems to be a bit faster).

If anyone could point out any neat strictness annotations or anything
else I could put in, that would be cool.

Cheers,

Malcolm


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

Message: 4
Date: Mon, 24 Nov 2008 02:32:52 +0200
From: "Christos Chryssochoidis" <[EMAIL PROTECTED]>
Subject: Re: [Haskell-beginners] Efficient sieve of erastothenes,       for
        solving project euler problem #10?
To: "Malcolm Reynolds" <[EMAIL PROTECTED]>
Cc: [email protected]
Message-ID:
        <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-1

Sorry, I pressed inadvertently the "send" button... The previous code
has some typos... :-)

The code is this:
------------------------------
isPrime :: Integer -> Bool
isPrime n = isPrimeAux n 2
    where
      -- First param is the number to be tested
      -- for primality. Second param the current
      -- divisor.
      isPrimeAux :: Integer -> Integer -> Bool
      isPrimeAux n k
          | fromIntegral k > sqrt (fromIntegral n) = True
          | otherwise  = if n `mod` k == 0 then
                             False
                         else
                             isPrimeAux n (k+1)


main :: IO ()
main = print $ sum $ filter isPrime [2..1999999]

-------------------------
I compiled the code with "ghc --make prob10.hs", and although it
wasn't lightning fast I got the answer after a little less than a
minute on my system.

Hope it helped,

Christos


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

Message: 5
Date: Sun, 23 Nov 2008 21:48:45 -0800
From: "David Frey" <[EMAIL PROTECTED]>
Subject: Re: [Haskell-beginners] Efficient sieve of erastothenes,       for
        solving project euler problem #10?
To: [email protected]
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-1

On 11/23/2008, "Malcolm Reynolds" <[EMAIL PROTECTED]> wrote:

>Hello all,
>
>I'm attempting to learn Haskell by going through the project euler
>problems. Number 10,
>http://projecteuler.net/index.php?section=problems&id=10 , involves
>summing prime numbers. It's easy in terms of coding something up that
>works, but I'm having a lot of trouble getting decent performance.
>I've learned a reasonable amount of ML at uni but Haskell is the first
>lazy language I've used.. I think the inefficiency is possibly due to
>the laziness but I'm not positive.
>
>I'd love if someone could show me how to do this in Haskell somewhere
>near as fast as C - at the moment I have a C version which runs in
>about a tenth of a second (
>http://github.com/malcster/project-euler-solutions--c-/tree/master/10.c
>). My haskell attempts are
>http://github.com/malcster/project-euler-solutions/tree/master/10better.hs
>(using the sieve) and
>http://github.com/malcster/project-euler-solutions/tree/master/10.hs
>(using possibly an even worse method, but seems to be a bit faster).
>
>If anyone could point out any neat strictness annotations or anything
>else I could put in, that would be cool.
>
>Cheers,
>
>Malcolm


Hi Malcom,

I have a solution to Project Euler problem #10 that runs in 7.3 seconds
on my computer when compiled with -O2.  I am neither a math expert nor a
Haskell expert, so others may be able to offer a better solution.



module PE010 where

import ProjectEuler(primes2)

main = putStrLn output

output = show result

result = sum $ takeWhile (\x -> x < 2000000) primes2

-- This is the relevant stuff from ProjectEuler.hs

primes2 :: [Integer]
primes2 = getPrime [] primeCandidates where
    getPrime :: [Integer] -> [Integer] -> [Integer]
    getPrime ls (x:xs) = let maxDiv = floor $ sqrt $ fromIntegral x in
        if isDivisibleByAny (takeWhile (\n -> n <= maxDiv) ls) x
        then getPrime ls xs
        else x : getPrime (ls ++ [x]) xs

primeCandidates = 2 : (oddsFrom 3)

oddsFrom n
    | odd n     = [n, n+2 ..]
    | otherwise = [n+1, n+3 ..]

isDivisibleByAny ls n = or $ map (\d -> n `mod` d == 0) ls

I didn't get a chance to look at your version, but obviously, 7.3
seconds is a lot slower than the 0.1 seconds you saw with your C version.


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

Message: 6
Date: Mon, 24 Nov 2008 11:14:08 +0100
From: apfelmus <[EMAIL PROTECTED]>
Subject: [Haskell-beginners] Re: Efficient sieve of erastothenes, for
        solving project euler problem #10?
To: [email protected]
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-1

Malcolm Reynolds wrote:
> I'm attempting to learn Haskell by going through the project euler
> problems. Number 10,
> http://projecteuler.net/index.php?section=problems&id=10 , involves
> summing prime numbers. It's easy in terms of coding something up that
> works, but I'm having a lot of trouble getting decent performance.

See also

  http://haskell.org/haskellwiki/Prime_numbers


Note that your C version uses a different algorithm than your Haskell
version, the former uses an array with random access while the latter
uses a linked list.


Regards,
apfelmus



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

Message: 7
Date: Mon, 24 Nov 2008 22:30:35 +1030
From: "Levi Stephen" <[EMAIL PROTECTED]>
Subject: [Haskell-beginners] GTK + Reactive
To: [email protected]
Message-ID:
        <[EMAIL PROTECTED]>
Content-Type: text/plain; charset="iso-8859-1"

Hi,

I'm trying to set up a simple program combining Reactive and GTK. I have the
program below so far, but mainQuit is never called. After looking at a
couple of adapters I tried adding the commented out line, but this didn't
work for me.

I'm sure I've probably missed something, but I'm not sure what.

Thanks,
Levi

main = do
 initGUI

 clock <- makeClock

 (windowDestroy, sink) <- makeEvent clock

 w <- windowNew
 w `onDestroy` sink ()
 widgetShowAll w

 -- forkE (tSync clock) $ quitOnDestroy windowDestroy

 mainGUI

quitOnDestroy :: Event () -> Event (IO ())
quitOnDestroy e = fmap (const mainQuit) e

tSync :: Clock TimeT-> ITime -> IO ()
tSync clock t = sleepPast (cGetTime clock) (exact t)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20081124/90a1b4ee/attachment-0001.htm

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

Message: 8
Date: Mon, 24 Nov 2008 09:23:49 -0500
From: "Kurt Hutchinson" <[EMAIL PROTECTED]>
Subject: Re: [Haskell-beginners] Trouble with formatting,       Real World
        Haskell example
To: [email protected]
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-1

On Sun, Nov 23, 2008 at 12:10 AM, Steven Ashley
<[EMAIL PROTECTED]> wrote:
> The contents of the do block must be indented at-least as much as
> mainWith on the previous line.

> main = mainWith myFunction
> ....where mainWith function = do
> ....................args <- getArgs


This is the reason that I format 'where' clauses like so:

main = mainWith myFunction
....where
....mainWith f = do
........args <- getArgs

etc. I think one more line of vertical space is worth all of the
horizontal space you end up saving.

Kurt


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

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


End of Beginners Digest, Vol 5, Issue 13
****************************************

Reply via email to