因此,我正在编写自己的解析器,该解析器快要完成了,但是我一直陷入函数返回的困境。我的返回值是case,但是在其中,case我必须进行解析,但无法使其正常工作。
parseCompontntsTile :: Tile -> Parser Tile
parseCompontntsTile (Tile pos fix wiel) =
do parseWhiteSpace
patern <- match "pos:" `mplus` match "fixed:" `mplus` match "wheel:"
return (case patern of
"pos" -> (Tile parsePosition fix wiel)
"fixed" -> (Tile pos parseFixed wiel)
"wheel" -> (Tile pos fix parseWiel) )
Run Code Online (Sandbox Code Playgroud)
功能parsePosition来自类型parsePosition :: Parser Coord; tile的构造函数为Coord Bool Bool。
当然,这是行不通的,因为会parsePosition返回Parser Coord并且期望返回Coord(没有“ parse”)。通常情况下,我只是将其“拆包”,但是,如何在一个包装箱内进行呢?
谢谢你的帮助
所以我想用它groupBy来根据snd元素对元组列表进行分组.每个具有相同snd元素的元组都应该在同一个列表中
group :: [(Int,Int)] -> [[(Int,Int)]]
group = groupBy (\a b -> snd a == snd b) lijst
Run Code Online (Sandbox Code Playgroud)
一个groupBy列表[(1,2),(8,9),(5,2),(9,2),(3,9),(1,1)]应该返回
[[(1,2),(5,2),(9,2)],[(8,9),(3,9)],[1,1]].
然而它又回来了 [[(1,2)],[(8,9)],[(5,2),(9,2)],[(3,9)],[(1,1)]].
如何使此功能起作用?
所以我想为我的对象type和data对象建立某种接口。所以我有
data Tile = Tile {
coord :: Coord,
fixed :: Bool,
wiel :: Bool
} deriving (Show)
type Source = (Coord , Bool)
type Sink = (Coord , Bool)
Run Code Online (Sandbox Code Playgroud)
并想为所有这些接口创建一个全局接口,所以我希望该接口具有另一个字段,rotating :: Bool然后带有the Tile或the Source或the Sink。
如果它们在哪里都实现了相同的接口,那么它们就具有额外的字段。这样,我也可以将它们放在我也需要的列表中。(也许是一个Nothing选择,以防万一我遍历列表时没有任何东西)。
我首先尝试这样做而没有像这样的多余字段
data newInterface = Source | Sink | Tile | Nothing
Run Code Online (Sandbox Code Playgroud)
但是,这无效,因为我得到了“ Tile多次定义”错误。
我将如何解决呢?谢谢
所以在我的项目中,我有一个返回数组的常量函数:
import System.Random
giveList :: [Int]
giveList = [8,9,4,5,2]
Run Code Online (Sandbox Code Playgroud)
我想从该列表中随机选择一个元素,如下所示:
seed::Int
seed = 40
generator = mkStdGen seed
giveRandomElement :: Int
giveRandomElement = giveList !! rand where
n = length tetrominoes
(rand,generator) = randomR (0,(n-1)) generator
Run Code Online (Sandbox Code Playgroud)
但是,由于生成器,这不能编译,我想将生成器保持为全局变量,所以我不必继续将它赋予函数.我也不想处理IO包装器,所以我能用这种方式做什么?
谢谢你的帮助:-)
所以我有这些常量FilePath变量(字符串)
s1 , s2 , s3 , s4 ... :: Filepath
s1 = "help.txt"
s2 = "sljdfn"
-- ...
Run Code Online (Sandbox Code Playgroud)
而且我有一个函数接受这些文件路径之一并返回一个int值。
positionInList:: Filepath -> Int
positionInList s1 = 1
positionInList s2 = 2
-- ...
Run Code Online (Sandbox Code Playgroud)
但是,在编译时会出现模式匹配冗余警告,并且程序运行异常,所以我认为这是问题所在。我将如何解决呢?