As I am trying to learn functional programming I have decided to do the advent of code challenges in haskell.
While doing challenge 5 https://adventofcode.com/2017/day/5 with input data https://adventofcode.com/2017/day/5/input I have encountered several problem.
This is my code
import Data.Array
import System.IO
listToArray l =
let n_elem = length l
pos_val = zip (range (0, n_elem)) l
in array (0, n_elem-1) pos_val
getData filename = do
s <- readFile filename
let l = map read (lines s) ::[Int]
a = listToArray l
return a
-- Part 1
updatePosArray i a =
let i_val = a ! i
in (i+i_val, a//[(i, i_val + 1)])
solution1 a n_steps i
| i >= length a || i < 0 = n_steps
| otherwise =
let ai = updatePosArray i a
in solution1 (snd ai) (n_steps+1) (fst ai)
-- Part 2
updatePosArray2 i a =
let i_val = a ! i
in
if i_val>=3 then (i+i_val, a//[(i, i_val-1)])
else (i+i_val, a//[(i, i_val+1)])
solution2 a n_steps i
| i >= length a || i < 0 = n_steps
| otherwise =
let ai = updatePosArray2 i a
in solution2 (snd ai) (n_steps+1) (fst ai)
main = do
x <- getData "/Users/lucapuggini/Documents/AdventOfCode/data/data_ch5_p1.txt"
let x_ex = array (0,4) [(0, 0), (1, 3), (2, 0), (3, 1), (4, -3)]
let n_steps_ex1 = solution1 x_ex 0 0
print $ n_steps_ex1
let n_steps1 = solution1 x 0 0
print $ n_steps1
let n_steps_ex2 = solution2 x_ex 0 0
print $ n_steps_ex2
-- very slow. Probably due to the immutable array
let n_steps2 = solution2 x 0 0
print $ n_steps2
and this is the result I get :
lucas-MacBook-Pro:src lucapuggini$ stack runhaskell challenge5.hs
5
381680
10
stack overflow
the code is quiet slow but this is probably expected due to the fact that I am using immutable array but I am surprised by the stack overflow error. I thought that this should not happen with tail recursion.
In conclusion I have 2 questions:
1) Why am I getting stackoverflow error? Am I using tail recursion wrongly?
2) What is a more efficient but still functional way to run this code? Are immutable array a bad choice?
I am very new to haskell so please be clear.
lucas-MacBook-Pro:src lucapuggini$ stack ghc challenge5.hs [1 of 1] Compiling Main ( challenge5.hs, challenge5.o ) Linking challenge5 ... lucas-MacBook-Pro:src lucapuggini$ ./challenge5 5 381680 10 29717847Data.IntMap.Strict.