Control.Monad.Reader

Everything About Mathematics, Functional Programming, and Haskell

Archive for May 2009

Finish reading of The Typeclassopedia

leave a comment »

Finish reading of The Typeclassopedia from The Monad.Reader 13, but it’s just the beginning of a series of reading about papers, articles, wikibooks, blog posts, and codes, as the author said.

It’s a great article, talks about Haskell’s type class and their relationship, has plenty of very useful references.

Advertisements

Written by Lee Duhem

May 30, 2009 at 11:27 am

Posted in Haskell

Tagged with , ,

Sieve of Eratosthenes In Haskell

leave a comment »

Algorithm description, see Sieve of Eratosthenes

module Main where

import System.Environment

isPrime p (x:xs)
	| x*x > p	 = True
	| p `mod` x == 0 = False
	| otherwise	 = isPrime p xs

primes = 2 : oprimes
	where oprimes = 3 : [ p | p <- [5,7..], isPrime p oprimes]

main = do
	args <- getArgs
	let	n = read $ args !! 0
		x = takeWhile (< n) primes
	print $ length x
	print x

There is a HackageDB package called primes which provides an efficient lazy wheel sieve for prime generation.

This version looks nicer, but less effective:

sieve (x:xs) = x : sieve xs'
	where xs' = filter (\y -> y `mod` x /= 0) xs

primes = 2 : sieve [3,5..]

See also The Genuine Sieve of Eratosthenes

Written by Lee Duhem

May 20, 2009 at 5:12 pm

Posted in Algorithm, Haskell, Mathematics

Tagged with , , ,