4

Essentially I have a function that uses Maybe Int's to display a Sudoku problem. The Solution to the Sudoku 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: Just would work, is there a way to convert a list of Int's into maybe Int's?

4

3 に答える 3

9

が で、代わりにが必要な場合xsは、を使用します。[Int][Maybe Int]map Just xs

于 2012-03-13T16:47:55.227 に答える
4

「ただ」をそのまま使います。

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

main = foobar (Just 3)

リストを変換するには、マップを使用するだけです。

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

Justリスト自体で行うと、 Maybe [Int].

于 2012-03-13T16:47:19.110 に答える
1

Preludeのsequence機能は、必要な機能とは正反対ですが、のsequenceバージョンは次のようにTraversable機能します。

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]

もちろんmap Just、あなたの場合はもっと簡単ですが、私のバージョンは、中にリストがある場合に便利ですJust

于 2012-03-13T20:59:18.227 に答える