
Hello all, As my first foray into the wonderful world of Haskell programming, I wrote a simple program that solves Laplace equations. The program is 44 lines, and I would greatly appreciate comments on how I could make it better, with regards to Haskell idioms, performance and readability. Cheers, Edward import Data.Array data Node = Node { isBoundary :: Bool, value :: Double } instance Show Node where show (Node True value) = show value ++ "*" show (Node False value) = show value blankArray :: Ix i => (i, i) -> a -> Array i a blankArray (a, b) x = array (a, b) [(i, x) | i <- range (a, b)] fillBox :: Ix i => (i, i) -> a -> Array i a -> Array i a fillBox (a, b) value m = m // [(i, value) | i <- range(a, b)] average :: Fractional a => [a] -> a average list = sum list / fromIntegral (length list) solve :: Array (Integer, Integer) Node -> Array (Integer, Integer) Node solve m = if maximum deltas > 0.0001 then solve n else n where n = array (min, max) [(i, solveNode i) | i <- range(min, max)] deltas = map delta $ zip (values m) (values n) delta (x, y) = abs (x - y) values a = map value $ elems a (min, max) = bounds m solveNode (a, b) = if isBoundary $ node then node else averageNode where node = m ! (a, b) averageNode = Node False $ average [value $ m ! i | i <- [(a+1,b),(a-1,b),(a,b+1),(a,b-1)]] myArray = fillBox ((3,3), (5,5)) (Node True 100) $ fillBox ((1,1), (8,8)) (Node False 0) $ blankArray ((0,0), (9,9)) (Node True 0)

2009/3/27 Edward Z. Yang
Hello all,
Hi
average :: Fractional a => [a] -> a average list = sum list / fromIntegral (length list)
There was a thread on this ML which was about an implementation of average which avoids traversing the list twice, one for 'sum' and one for 'length'. You could use it, if you care for performance and your list is long enough to matter Ciao ---------- FB

On Sat, 28 Mar 2009, Francesco Bochicchio wrote:
There was a thread on this ML which was about an implementation of average which avoids traversing the list twice, one for 'sum' and one for 'length'. You could use it, if you care for performance and your list is long enough to matter
The list is four elements long, so I don't particularly care. Although, I wonder why there isn't a built-in average function. Cheers, Edward

On Sat, Mar 28, 2009 at 6:09 PM, Edward Z. Yang
The list is four elements long, so I don't particularly care. Although, I wonder why there isn't a built-in average function.
Indeed, since we do have maximum and minimum on a list... Of course where does it end, one could imagine ending up with a big statistics package :)

On Sun, 29 Mar 2009 20:27:34 +0200, Peter Verswyvelen
On Sat, Mar 28, 2009 at 6:09 PM, Edward Z. Yang
wrote: The list is four elements long, so I don't particularly care. Although, I wonder why there isn't a built-in average function.
Indeed, since we do have maximum and minimum on a list... Of course where does it end, one could imagine ending up with a big statistics package :)
And of course there are statistics packages; hstats (in Hackage) contains an average function, capable of handling large lists. -- Met vriendelijke groet, Henk-Jan van Tuyl -- http://functor.bamikanarie.com http://Van.Tuyl.eu/ --
participants (4)
-
Edward Z. Yang
-
Francesco Bochicchio
-
Henk-Jan van Tuyl
-
Peter Verswyvelen