0

What I am trying to do is make a program that will go through a two dimensional square of numbers and find the path that gives the highest total. Starting from the top, valid moves are down, and down left/right if available.

The problem that I am having is I am using this function:

  slice :: [Int] -> Int -> Int -> [Int]
  slice x i k 
    | i > k = []
    | otherwise = (take (k-i+1) (drop (i-1) x))

and this function:

    path :: Board -> [Int]
    path [] = []
    path (x:xs) = (maximum (slice x 3 5 ) : path xs

where a type Board is a list of lists.

Could anyone explain to me how I could use variables instead of the 3 and 5, this way I could get all the combos, and then evaluate which is the best route.

1
  • there is a great tool hlint that helps with indentation and ()-errors - cabal install hlint Commented Feb 11, 2012 at 23:36

1 Answer 1

1

Newbie's first attempt :-)

path::[Board] -> Int -> Int -> [Int]
path [] y z = []
path (x:xs) y z = maximum(slice x y z) : path xs y z
Sign up to request clarification or add additional context in comments.

4 Comments

thank you.. Now I'm trying to find the width of the square and make the function cycle through all the possible moves. I need something like a loop, that'll go 1-3, 2-4 ect.
in haskell you rather use recursion or fold/scan for iteration than loops
yeah, im just having trouble finding the right recursive function.
@user1204349 - I suggest in that case to start a different thread with that question. And remember to accept the answer if it satisfies your question. Don't overload threads ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.