Haskell:嵌套列表理解

Haskell:嵌套列表理解,haskell,Haskell,我有一个学校作业,需要帮助。到目前为止,我已经根据赋值指令创建了两种类型,参数和谓词 在这个项目中,我必须创建一个名为“上下文”的列表,其中包含世界上的参数(或对象),以及一个名为“事实”的列表中有关这些对象的事实 例如,上下文列表有参数“john”和“boston”,然后在事实列表中,我们可以使用函数fly创建一个谓词,使事实“fly john to_boston”,其中to表示john飞往波士顿 对于项目的最后一步,我们必须能够询问Haskell:“qWhere fly john”,让Has

我有一个学校作业,需要帮助。到目前为止,我已经根据赋值指令创建了两种类型,
参数
谓词

在这个项目中,我必须创建一个名为“上下文”的列表,其中包含世界上的参数(或对象),以及一个名为“事实”的列表中有关这些对象的事实

例如,上下文列表有参数“john”和“boston”,然后在事实列表中,我们可以使用函数
fly
创建一个谓词,使事实“fly john to_boston”,其中
to
表示john飞往波士顿

对于项目的最后一步,我们必须能够询问Haskell:“qWhere fly john”,让Haskell搜索上下文列表中的“john”,并使用该列表搜索事实列表中的“fly”和“john”,以便最终返回“波士顿”

我知道这是嵌套列表理解,但我不知道如何让Haskell在有“fly john”后返回“to_boston”。我将在下面列出一些代码(滚动到底部查看我一直在做的工作):

{-#语言多段类型类}
--GL类型
数据类型=HUMN |--人类
动画——制作动画
有机物
组织机构
PHYS |——物理对象
ARTF |——人工制品
EVNT |——事件
提案|——命题
信息|——信息
感觉
LOCA |——位置
时间|——时间段
注意——态度
情绪——情绪
PPTY |--属性
OBLG |——义务
规则——规则
派生(显示、等式、枚举)
--自定义数据类型
数据参数=参数{ttype::Type,value::String}
推导(显示,等式)
数据谓词=谓词{引理::字符串
,参数::[Argument]}
推导(显示,等式)
类型上下文=[参数]
--创建语义类型的参数,如下所示
日期::字符串->参数
日期s=参数{ttype=TIME,值=s}
时间::字符串->参数
时间s=参数{ttype=time,value=s}
位置::字符串->参数
位置s=参数{ttype=LOCA,值=s}
人::字符串->参数
人类s=参数{ttype=HUMN,value=s}
phys::字符串->参数
phys s=参数{ttype=phys,value=s}
工件::字符串->参数
工件s=参数{ttype=ARTF,值=s}
动画::字符串->参数
动画s=参数{ttype=ANIM,value=s}
--创建实体/PP,如下所示
5月15日=日期“2014年5月15日”
sevenAM=时间“7:00”
sandiego=地点“圣地亚哥”
约翰=人类的“约翰”
玛丽=人类的“玛丽”
波士顿=地点“波士顿”
球=物理“球”
汽车=人工制品“汽车”
cat=设置“cat”的动画
鼠标=设置“鼠标”的动画
去波士顿
上下文=[
5月15日,
塞维南,
桑迪戈,
厕所,
玛丽,
波士顿,
球,
猫,
老鼠
]
--辅助函数
getValue::参数->字符串
getValue c=值c
getType::参数->类型
getType c=t类型c
isType::参数->类型->布尔
isType c t=(t TYPE c==t)
--按如下方式创建介词
to::Argument->Predicate
to x=谓词{lemma=“to”,参数=[x]}
--创建如下动词
a班b班在哪里
fly::a->b->谓词
实例飞行参数参数,其中
fly x y=谓词{lemma=“fly”,参数=[x,y]}
--覆盖引理,
实例飞行参数谓词,其中
fly x y=谓词{引理=引理y
,arguments=[x,arguments y!!0]}
事实=[约翰飞往波士顿,玛丽飞往波士顿]
--这就是我被困的地方\/
qWhere::(参数->参数->谓词)->参数
->[[论点]]

其中fx=[[arguments z |]| z我认为您不需要/想要嵌套列表理解。首先,您需要理解这实际上只是语法上的糖分

但是我们可以使用
let…in
语法来利用多个列表理解。解决方案可能如下所示:

qWhere :: (Argument -> Argument -> Predicate)
       -> Argument
       -> [[Argument]]
qWhere f x = case find (== x) context of
  Just e ->
        -- first we get all facts about e.g. john
    let personFacts = [z | z <- facts, e `elem` arguments z]
        -- then we get all facts when we apply f to john and
        -- any other arguments that exist in john
        actionFacts = fmap (f e) (concatMap arguments personFacts)
        -- and extract all arguments of those facts
        actionArgs  = concatMap arguments actionFacts
        -- and can finally build the actual list of facts,
        -- reduced by checking if the argument "john" is in one of our
        -- actionArgs where we applied f to
    in map arguments [z | z <- personFacts, e `elem` actionArgs]
  Nothing -> []
qWhere::(参数->参数->谓词)
->论据
->[[论点]]
其中,f x=案例查找(=x)上下文
只是e->
--首先,我们了解有关约翰的所有事实

让personFacts=[z | z你能给出一个具体的例子,说明调用
qWhere
应该返回什么吗?例如
qWhere fly boston=?
qWhere :: (Argument -> Argument -> Predicate)
       -> Argument
       -> [[Argument]]
qWhere f x = case find (== x) context of
  Just e ->
        -- first we get all facts about e.g. john
    let personFacts = [z | z <- facts, e `elem` arguments z]
        -- then we get all facts when we apply f to john and
        -- any other arguments that exist in john
        actionFacts = fmap (f e) (concatMap arguments personFacts)
        -- and extract all arguments of those facts
        actionArgs  = concatMap arguments actionFacts
        -- and can finally build the actual list of facts,
        -- reduced by checking if the argument "john" is in one of our
        -- actionArgs where we applied f to
    in map arguments [z | z <- personFacts, e `elem` actionArgs]
  Nothing -> []