【问题标题】:How do I put a boolean into a list and output it in Haskell?如何将布尔值放入列表并在 Haskell 中输出?
【发布时间】:2023-02-06 00:47:09
【问题描述】:

所以我是 Haskell 的新手,下面我尝试编写一个函数,该函数采用给定的 e 值和给定的列表,并确定给定值是否出现在给定的列表中,如果给定的值确实出现则输出 True,否则输出 False。

inListm e [] = False 
inListm e (x:xs)
 | e == x = True || inListm e xs
 | otherwise = False || inListm e xs 

如果

inListm 2 [0, 2, 1, 2] 

给出,输出将是

True

但是,我希望最终输出在这样的列表中

[True]

我试图通过

inListd e [] =  False : []
inListd e (x:xs)
  | e == x = True : [] || inListd e xs
  | otherwise = False :[]  || inListd e xs 
  

但是给我的只是一个错误,所以我想知道如何解决这个问题

【问题讨论】:

    标签: list haskell boolean


    【解决方案1】:

    你走在正确的轨道上。如果你有结果,唯一需要做的就是返回一个列表,所以:

    inListm :: Eq a => a -> [a] -> [Bool]
    inListm e [] = [False] 
    inListm e (x:xs)
     | e == x = [True]
     | otherwise = inListm e xs
    

    话虽这么说,但将结果包装在列表中似乎没有多大意义。

    【讨论】:

      猜你喜欢
      • 2015-04-07
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 2011-11-28
      • 2015-07-03
      相关资源
      最近更新 更多