Re: [Haskell-cafe] how to optmize this code?

2011-04-11 Thread Gilberto Garcia
Hi Guys, Thanks all for the suggestions, I have certainly improved my knowledge. I made a blog post to show all the possible solution a problem can have. you can check it out at katacoder.blogspot.com Giba On Sun, Apr 10, 2011 at 3:35 AM, Johan Tibell wrote: > Hi Gilberto, > > On Wed, Mar 30, 2

Re: [Haskell-cafe] how to optmize this code?

2011-04-09 Thread Johan Tibell
Hi Gilberto, On Wed, Mar 30, 2011 at 4:39 PM, Gilberto Garcia wrote: > fkSum :: Int -> [Int] -> Int > fkSum a [] = 0 > fkSum a (b) = foldl (+) 0 (filter (\x -> isMultiple x b) [1..a]) > > isMultiple :: Int -> [Int] -> Bool > isMultiple a [] = False > isMultiple a (x:xs) = if (mod a x == 0) then T

Re: [Haskell-cafe] how to optmize this code?

2011-04-09 Thread Henning Thielemann
Gilberto Garcia schrieb: > isMultiple :: Int -> [Int] -> Bool > isMultiple a [] = False > isMultiple a (x:xs) = if (mod a x == 0) then True else isMultiple a xs I think this one can be written in terms of 'List.any'. ___ Haskell-Cafe mailing list Haske

Re: [Haskell-cafe] how to optmize this code?

2011-03-31 Thread Ryan Ingram
On Thu, Mar 31, 2011 at 7:29 AM, Daniel Fischer < daniel.is.fisc...@googlemail.com> wrote: > Err, terminology problem here. > Strictly speaking, a function is strict iff > > f _|_ = _|_ > > while we are talking here about evaluation strategies, so we should better > have spoken of eager vs. deferr

Re: [Haskell-cafe] how to optmize this code?

2011-03-31 Thread Daniel Fischer
On Thursday 31 March 2011 14:27:59, Yves Parès wrote: > Just to be sure, because I am not quite familiar with the dark hairy > > internals of GHC: > > Of course, given a type signature that allows strictness to be > > inferred. > > You mean a signature with no type variables and types that are kn

Re: [Haskell-cafe] how to optmize this code?

2011-03-31 Thread Yves Parès
Just to be sure, because I am not quite familiar with the dark hairy internals of GHC: > Of course, given a type signature that allows strictness to be inferred. You mean a signature with no type variables and types that are know to GHC as being strict? (Like Int -> Int -> Int instead of (Num a)

Re: [Haskell-cafe] how to optmize this code?

2011-03-31 Thread Daniel Fischer
On Thursday 31 March 2011 11:45:00, Christian Maeder wrote: > Since we don't have a function sum' in the Prelude (should we have it?) I think we should. > I wonder what happens if you just use "sum". Will the "sum" (based on > sum' so without -DUSE_REPORT_PRELUDE) be strict enough? I don't kno

Re: [Haskell-cafe] how to optmize this code?

2011-03-31 Thread Christian Maeder
Am 31.03.2011 05:59, schrieb Felipe Almeida Lessa: On Wed, Mar 30, 2011 at 2:39 PM, Gilberto Garcia wrote: fkSum :: Int -> [Int] -> Int fkSum a [] = 0 fkSum a (b) = foldl (+) 0 (filter (\x -> isMultiple x b) [1..a]) Daniel Fischer and Yves Parès gave you good suggestions about implementing

Re: [Haskell-cafe] how to optmize this code?

2011-03-30 Thread Gilberto Garcia
Thank you very much for the suggestions. giba ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Re: [Haskell-cafe] how to optmize this code?

2011-03-30 Thread Felipe Almeida Lessa
On Wed, Mar 30, 2011 at 2:39 PM, Gilberto Garcia wrote: > fkSum :: Int -> [Int] -> Int > fkSum a [] = 0 > fkSum a (b) = foldl (+) 0 (filter (\x -> isMultiple x b) [1..a]) Daniel Fischer and Yves Parès gave you good suggestions about implementing a different, better algorithm for you problem. How

Re: [Haskell-cafe] how to optmize this code?

2011-03-30 Thread Yves Parès
If I'm not wrong : sum [1..n] = (n² + n)/2 2011/3/30 Daniel Fischer > On Wednesday 30 March 2011 16:39:49, Gilberto Garcia wrote: > > Hi Haskellers, > > > > I was solving this problem from project euler to study haskell. > > I came up whit the following solution and I was wondering if there is

Re: [Haskell-cafe] how to optmize this code?

2011-03-30 Thread Daniel Fischer
On Wednesday 30 March 2011 16:39:49, Gilberto Garcia wrote: > Hi Haskellers, > > I was solving this problem from project euler to study haskell. > I came up whit the following solution and I was wondering if there is > a more optimized and concise solution. Yes. There's a constant-time formula fo

[Haskell-cafe] how to optmize this code?

2011-03-30 Thread Gilberto Garcia
Hi Haskellers, I was solving this problem from project euler to study haskell. I came up whit the following solution and I was wondering if there is a more optimized and concise solution. fkSum :: Int -> [Int] -> Int fkSum a [] = 0 fkSum a (b) = foldl (+) 0 (filter (\x -> isMultiple x b) [1..a])