[Haskell-cafe] Project Euler: request for comments
KC
kc1956 at gmail.com
Wed Aug 31 01:19:25 CEST 2011
You also don't need mutual recursion for this explicit recursion since
I imagine it would use up more stack space.
-- Doing the one dimensional case.
f011 :: [Int] -> Int
f011 (t:u:v:xs) = f011helper t u v xs
f011helper :: Int -> Int -> Int -> [Int] -> Int
f011helper t u v (w:ws)
| ws == [] = t*u*v*w
| otherwise = max (t*u*v*w) (f011helper u v w ws)
-- Note: f011helper does not call f011.
On Mon, Aug 29, 2011 at 9:40 AM, Oscar Picasso <oscarpicasso at gmail.com> wrote:
> Got it.
>
> f :: [Int] -> Int
> f (t:u:v:xs) = helper t u v xs
>
> helper :: Int -> Int -> Int -> [Int] -> Int
> helper t u v (w:ws)
> | ws == [] = t*u*v*w
> | otherwise = max (t*u*v*w) (f (u:v:w:ws))
>
> I tend to overlook mutual recursion in my toolbox.
Good going! :)
>
> Thanks for the enlightenment.
>
> On Sun, Aug 28, 2011 at 4:54 PM, KC <kc1956 at gmail.com> wrote:
>> Try something like the following:
>>
>> -- Project Euler 11
>>
>> -- In the 20×20 grid below, four numbers along a diagonal line have
>> been marked in red.
>>
>> -- <snip>
>>
>> -- The product of these numbers is 26 × 63 × 78 × 14 = 1788696.
>>
>> -- What is the greatest product of four adjacent numbers in any
>> direction (up, down, left, right, or diagonally) in the 20×20 grid?
>>
>>
>> import Data.List
>>
>> -- Doing the one dimensional case.
>> f011 :: [Int] -> Int
>> f011 (t:u:v:xs) = f011helper t u v xs
>>
>> f011helper :: Int -> Int -> Int -> [Int] -> Int
>> f011helper t u v (w:ws)
>> | ws == [] = t*u*v*w
>> | otherwise = yada nada mada
>>
>> -- What are yada nada mada?
>>
>> -- The 20x20 grid case will become:
>> f0112D :: [[Int]] -> Int
>> -- where [[Int]] is a list of lists of rows, columns, major diagonals,
>> & minor diagonals.
>>
--
--
Regards,
KC
More information about the Haskell-Cafe
mailing list