Haskell Int and Maybe Int

Essentially, I have a function that uses Maybe Int to display the Sudoku problem. Sudoku's solution contains only Ints, and the code for displaying the Grid will be almost identical, with the exception of Maybe Int, used for problems and Int, used for solutions.

Is it possible to use Int values ​​for a function that requires Maybe Int, if so, how?

Edit: it just works, is there a way to convert an int list to possibly an int?

+4
source share
3 answers

If xs is [Int] , and instead you want [Maybe Int] , use map Just xs .

+9
source

Just use "Just."

 foobar :: Maybe Int -> IO () foobar x = print x main = foobar (Just 3) 

To convert a list, you can simply use the map.

 maybeList :: [a] -> [Maybe a] maybeList = map Just 

If you execute Just in the list itself, you will get Maybe [Int] .

+4
source

The sequence function in Prelude does the same thing you need, but the sequence version in Traversable works for you:

 import Data.Traversable as T T.sequence $ Just [1..10] --[Just 1,Just 2,Just 3,Just 4,Just 5,Just 6,Just 7,Just 8,Just 9,Just 10] 

Of course, map Just easier in your case, but my version is convenient if you have a list inside Just .

+1
source

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


All Articles