[Haskell-cafe] Project Euler: request for comments
Oscar Picasso
oscarpicasso at gmail.com
Wed Aug 31 22:38:32 CEST 2011
In that case, maybe we could just get rid of the helper function and just write:
g (t:u:v:w:[]) = t*u*v*w
g (t:u:v:w:ws) = max (t*u*v*w) (g (u:v:w:ws))
By the way, what make you think that mutual recursion would use more
stack space than single recursion?
Oscar
On Tue, Aug 30, 2011 at 7:19 PM, KC <kc1956 at gmail.com> wrote:
> 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