Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 初学者问题:在自定义类型的两个列表的每个组合上使用函数的问题_List_Haskell_Functional Programming - Fatal编程技术网

List 初学者问题:在自定义类型的两个列表的每个组合上使用函数的问题

List 初学者问题:在自定义类型的两个列表的每个组合上使用函数的问题,list,haskell,functional-programming,List,Haskell,Functional Programming,我目前正在准备一门入门级大学课程的考试,在这门课程中,我们主要使用Haskell来模拟组合电路。我对Haskell本身有一些基本的问题,对函数式编程几乎一无所知,并且对Java有基本的理解,这使事情更加复杂 我正在尝试构建一个函数,该函数在一个比较器中使用并打印我自定义类型的两个列表的所有组合。我正在处理以下(按预期)代码片段: type Nibble = (Bool, Bool, Bool, Bool) comparator :: Nibble -> Nibble -> Bool

我目前正在准备一门入门级大学课程的考试,在这门课程中,我们主要使用Haskell来模拟组合电路。我对Haskell本身有一些基本的问题,对函数式编程几乎一无所知,并且对Java有基本的理解,这使事情更加复杂

我正在尝试构建一个函数,该函数在一个比较器中使用并打印我自定义类型的两个列表的所有组合。我正在处理以下(按预期)代码片段:

type Nibble = (Bool, Bool, Bool, Bool) 
comparator :: Nibble -> Nibble -> Bool 
comparator (a3,a2,a1,a0) (b3,b2,b1,b0) = nor [a3<+>b3, a2<+>b2, a1<+>b1, a0<+>b0]
我有一个较小的例子。在这里,我们检查了XOR运算符的定义是否按预期工作。我试图确定手头任务的类型签名必须是什么样子,并编写适当的函数来模仿下面的示例,但我迷路了:

(<+>) :: Bool -> Bool -> Bool
(<+>) a b = (a || b) && (not (a && b))

table_row :: (Bool -> Bool -> Bool) -> (Bool, Bool) -> String
table_row f (x, y) = show (x,y)++ ":" ++ show (f x y)

table :: (Bool -> Bool -> Bool) -> [(Bool, Bool)] -> String
table f [] = ""
table f (x:xs) =  table_row f x ++ "\n" ++ table f xs

checkTable = do
  let bool_duo [(a,b) | a <- [False,True], b <- [False,True]]
  putStrLn (table (<+>) bool_duo)
()::Bool->Bool->Bool
()AB=(a | b)和&(非a和b))
表格行::(Bool->Bool->Bool)->(Bool,Bool)->字符串
表_第f(x,y)行)=显示(x,y)+“:“++显示(f x y)
表::(Bool->Bool->Bool)->[(Bool,Bool)]->字符串
表f[]=“”
表f(x:xs)=表_行f x++“\n”+++表f xs
检查表

让bool_duo[(a,b)| a你已经看到了列表的理解。你可以用它来调用一个包含列表中所有元素的函数

results :: [String]
results = 
   [ "The square of " ++ show x ++ " is " ++ show (square x)
   | x <- allInputs ]
   where
   square y  = y*y
   allInputs = [0..10]  -- an example

如果您不想要字符串列表,请相应地进行调整。

很难确定您想要做什么,但我最好的猜测是这样的:

bools = [True, False]
all_nibbles = [(a,b,c,d) | a <- bools, b <- bools, c <- bools, d <- bools]
all_results = [comparator x y | x <- all_nibbles, y <- all_nibbles]
bools=[True,False]

all_nibbles=[(a,b,c,d)| a不清楚您有什么输入和想要得到什么输出。这两个列表在哪里?我在我的帖子中添加了这一点。总结一下:我试图获得我自定义类型的两个实例的每一个可能组合的比较器函数的输出。一个完美的输出应该是:(真,真,假,假)(对,对,错,对):错我仍然不清楚;我不明白你对问题的描述与“完美输出”有什么关系我建议您添加一些Java/Python/pseudocode实现,以确保它包含以明确的形式解决问题所需的所有信息。我不明白您试图组合的两个列表是什么,以及您试图获得的输出是什么从中。您的问题不包含两个列表。它包含一个列表,并且不是输入。我希望出现这种格式的问题:我的第一个输入列表是
[…]
,我的第二个输入列表是
[…]
,我的预期输出是
..
。您在过程中尝试的任何代码都是额外的奖励。
results :: [String]
results = 
   [ "The comparison of " ++ show x ++ " and " ++ show y
     ++ " is " ++ show (comparator x y)
   | x <- allInputs, y <- allInputs ]
   where
   allInputs = ....  -- your list with all the inputs to try
bools = [True, False]
all_nibbles = [(a,b,c,d) | a <- bools, b <- bools, c <- bools, d <- bools]
all_results = [comparator x y | x <- all_nibbles, y <- all_nibbles]