HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Hackerrank, Utopian Tree in Haskell

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
hackerrankhaskelltreeutopian

Problem

Problem statement


The Utopian tree goes through 2 cycles of growth every year. The first
growth cycle occurs during the monsoon, when it doubles in height. The
second growth cycle occurs during the summer, when its height
increases by 1 meter. Now, a new Utopian tree sapling is planted at
the onset of the monsoon. Its height is 1 meter. Can you find the
height of the tree after \$N\$ growth cycles?


Input Format


The first line contains an integer, \$T\$, the number of test cases.
\$T\$ lines follow. Each line contains an integer, \$N\$, that denotes
the number of cycles for that test case.


Constraints

1 <= T <= 10 
0 <= N <= 60




Sample Input: #01:

2
3
4




Sample Output: #01:

6
7




Explanation: #01:


There are 2 testcases.


N = 3:

* the height of the tree at the end of the 1st cycle = 2
* the height of the tree at the end of the 2nd cycle = 3
* the height of the tree at the end of the 3rd cycle = 6


N = 4:



  • the height of the tree at the end of the 4th cycle = 7




This is my accepted solution:

module Main where

import Control.Monad(mapM_)

main = do
    let base_height = 1
    n_test_cases  putStrLn $ show $ utopianTreeHeight base_height 
        (test_cases !! i)) [0..n_test_cases - 1]

getList :: Int -> IO [Int]
getList n = 
    if n == 0
        then return []
    else
        do
            x  Int -> Int
utopianTreeHeight present_height 0 = present_height
utopianTreeHeight present_height cycles = 
    if odd cycles
        then 2 * (utopianTreeHeight present_height (cycles - 1))
    else
        1 + (utopianTreeHeight present_height (cycles - 1))


So, does this solution show good Haskell style? What could be improved? I have doubts especially regarding the way I deal with the IO (which was not provided by Hackerrank), as it was the first time I read about and applied the Haskell way of doing it.

Solution

Your solution lacks the use of higher-order functions that are an essential part of Haskell style. Instead of writing explicitly recursive functions that plumb state around (like an impoverished for-loop) seek out, create, and use constructs that have semantic meaning to your problem.

Starting from the small, the problem defines a growth cycle as a either doubling a tree's height, or increasing it by one. We can encode all of that information in Haskell.

type Height = Integer
type Cycle = Height -> Height

monsoon :: Cycle
monsoon = (* 2)

summer :: Cycle
summer = (+ 1)


Next, we know that the cycles begin with a monsoon and thereafter proceed regularly indefinitely into the future.

timeline :: [Cycle]
timeline = cycle [monsoon, summer]


cycle is a function from the Haskell Prelude module which turns a finite list into an infinite list by infinitely repeating it. Now we can take a finite sequence from the front of the list to determine all of the cycles the tree will experience over a given span of time.

-- code fragment
take n timeline :: [Cycle] -- n cycles from planting


Now given the original height of the tree and the cycles it will experience over a given span of time, we can find the resulting height of the tree.

utopian :: Int -> Height
utopian n = foldl (flip ($)) 1 $ take n timeline


We fold over the finite timeline from the left, applying each growth function to the accumulator (starting with the height of the tree at year 0).

Code Snippets

type Height = Integer
type Cycle = Height -> Height

monsoon :: Cycle
monsoon = (* 2)

summer :: Cycle
summer = (+ 1)
timeline :: [Cycle]
timeline = cycle [monsoon, summer]
-- code fragment
take n timeline :: [Cycle] -- n cycles from planting
utopian :: Int -> Height
utopian n = foldl (flip ($)) 1 $ take n timeline

Context

StackExchange Code Review Q#86208, answer score: 3

Revisions (0)

No revisions yet.