从Haskell的匿名函数真值表 [英] Truth Tables from Anonymous Functions in Haskell

查看:204
本文介绍了从Haskell的匿名函数真值表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个真值表定的boolean前pression。我可以创建一个新的数据类型BoolExpr做到这一点,但我想用一个匿名函数来做到这一点。它应该像这样的工作:

I'm trying to generate a truth table for a given boolean expression. I could do this with creating a new Datatype BoolExpr, but I want to do it with an anonymous function. It's supposed to work like this:

> tTable (\x y -> not (x || y))
output:
F F | T
F T | F
T F | F
T T | F

我的方法:

tbl p = [(uncurry p) tuple | tuple <- allval]
        where allval=[(x,y) | x <- [False,True], y <- [False,True]]

这工作,但仅适用于2个参数。我想这样做任何数量的参数的。所以,我想我会做一个函数,从列表中取参数:

This works, but only for 2 Arguments. I want to do it for any number of Arguments. So I figured I would make a function that takes the Arguments from a List:

argsFromList f []     = f
argsFromList f (x:xs) = argsFromList (f x) xs

这不工作:

 Occurs check: cannot construct the infinite type: t = t1 -> t
   Expected type: t -> [t1] -> t1 -> t
   Inferred type: (t1 -> t) -> [t1] -> t1 -> t
 In the expression: argsFromList (f x) xs

我不明白的问题是在这里。
我会很感激,如果任何人都可以点我到正确的方向,或张贴一个链接,做。

I don't understand what the problem is here. I would be very grateful if anyone could point me into the right direction or post a link that does.

推荐答案

这里的问题是,你要递归调用函数与不同类型的递归步骤。考虑定义:

The problem here is that you're trying to call a function recursively with a different type for the recursive step. Consider the definition:

argsFromList f []     = f
argsFromList f (x:xs) = argsFromList (f x) xs

让我们尝试推断类型自己。马上就可以看到,第一个参数˚F应该是至少有一个参数的函数,第二个参数(X:XS)是一个列表,列表中的元素应该是同一类型 F的第一个参数。在第一种情况下的说法˚F返回,所以最终的返回类型必须是相同的第一个参数。因此,我们开始与此:

Let's try to infer the type ourselves. We can immediately see that the first argument f should be a function of at least one argument, the second argument (x:xs) is a list, and the list elements should be the same type as the first argument of f. In the first case the argument f is returned, so the final return type must be the same as the first argument. So we start with this:

argsFromList :: (a -> ?) -> [a] -> (a -> ?)

要找到未知类型,大家可以看一下第二种情况下,它由一个递归调用。参数 XS 是同一个列表类型和参数(FX)已键入。因为它被用作递归调用的第一个参数,其类型为(A - &GT;?),我们现在可以得出结论, <? / code>是同一类型(A - &GT;?)所以这是同类型(A - &GT;(一个 - &GT;))所以这是同类型(A - &GT;(一 - &GT;?(A - &GT;)))这是......哎呀。

To find the unknown type ?, we can look at the second case, which consists of a recursive call. The argument xs is the same list type, and the argument (f x) has type ?. Since it's being used as the first argument in the recursive call, which has type (a -> ?), we can now conclude that ? is the same type as (a -> ?) which is therefore the same type as (a -> (a -> ?)) which is therefore the same type as (a -> (a -> (a -> ?))) which is... oops.

这将是无限型,当然。

如果你想使用可变数量的单一类型的参数的函数要做到这一点,你可能想使用取值的列表,而不是单个参数的函数。否则,你将不得不单独写每个版本或者使用一些晦涩难懂的技巧涉及高级语言功能,这两者都不在一个简单的情况下,这样的吸引力。

If you want to do this with functions that use a variable number of arguments of a single type, you'll probably want to use functions that take a list of values rather than individual arguments. Otherwise, you'll have to either write each version individually or use some arcane tricks involving advanced language features, neither of which is appealing in a simple case like this.

这篇关于从Haskell的匿名函数真值表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆