mercredi 2 juin 2021

Haskell: if-then-else conditioning problem

I am fairly new to Haskell and trying to comprehend writing functions and if else conditions and everything else. I am trying to write a very basic function but I don't fully understand if-then-else usage. I have a maze that i represent as [[Char]]. And this function will simply look at the position x,y in the maze and returns if it's a valid position or not. (whether it's in the maze boundaries or not)

I have written this so far:

is_valid_position :: Int -> Int -> [[Char]] -> Bool

is_valid_position x y maze
        if(x < 0 || y < 0)
            then False
        if(x >= length maze || y >= length (maze!!0))
            then False
        else True

This gives an error because of the 'else' usage right now. What I'm trying to write in python is like this:

def is_valid_position(maze, pos_r, pos_c):
    if pos_r < 0 or pos_c < 0:
        return False
    if pos_r >= len(maze) or pos_c >= len(maze[0]):
        return False
    
    return True

How should I change my Haskell code? I appreciate any help.

Aucun commentaire:

Enregistrer un commentaire