What does this Haskell Data.Maybe function do?

I would like to ask if you know what this function does:

hasUnassigned :: [Int] -> Bool hasUnassigned board = isJust $ elemIndex 0 board 

I found this feature on the net, and I plan to use it to help solve Sudoku gaming solutions.

Please explain to me how this works?

+4
source share
2 answers

elemIndex searches for the first argument in the second argument, which is a list. It returns the Maybe Int - Just index if the element is found, Nothing otherwise.

isJust returns true if Maybe is equal to Just , false if it is Nothing .

In other words, this is a very inconvenient way to check if board contains 0. The best way is

 hasUnassigned board = 0 `elem` board 
+11
source

elemIndex has the signature a -> [a] -> Maybe Int . When applied to x and the list it gives us Just i if x appears for the first time in list at position i . If x not in list , we get Nothing instead.

The external function isJust takes a Maybe a and tells us whether it is a Just whatever or Nothing form. hasUnassigned thus checks to see if there is 0 in the list of boards.

+2
source

Source: https://habr.com/ru/post/1486043/


All Articles