English 中文(简体)
如何比较两个职能
原标题:How to do the Comparison of Two Functions
  • 时间:2011-10-06 10:29:28
  •  标签:
  • sml

I would like to know how to compare two function F(x) & G(x) in SML, which two functions must return the same value that f(x)==g(x), where 1<= x <= 100. For example:

- fun f x = x*x;    
val f = fn : int -> int
- fun g x = x+x;
val g = fn : int -> int
- iden f g;
val it = false : bool
- fun f x = x*x;
val f = fn : int -> int
- fun g x = if x<0 then 0 else x*x;
val g = fn : int -> int
- iden f g;
val it = true : bool
问题回答

由于对所有投入而言,对两项职能(表格)是否平等进行测试是无法计算的,因此,你的<代码>iden功能可能必须比对其比较的两种功能更具参数。

总的说来,<代码>iden 将:

- fun iden f g elem = f(elem) = g(elem)
val iden = fn : ( a ->   b) -> ( a ->   b) ->  a -> bool

在你的具体情况下,你可能希望这样做:

- fun iden f g = let
=   fun iden_h f g (a, b) =
=     if a > b then iden_h f g (b, a)
=     else if a = b then f(a) = g(a)
=     else f(a) = g(a) andalso iden_h f g (a+1, b)
=  in
=   iden_h f g (1, 100)
=  end
val iden = fn : (int ->   a) -> (int ->   a) -> bool
-
- iden (fn x => x + x) (fn x => 2 * x);
val it = true : bool
- iden (fn x => x + x) (fn x => x * x);
val it = false : bool




相关问题
Standard ml function in datatype problem

I have to create a function about peano numbers defined as the following datatype: datatype a peano = P of ( a -> a) * a -> a val zero = P(fn (f, x) => x) The function that I have to ...

convert string to list in Standard ML [duplicate]

Possible Duplicate: Open file in ML(SMLNJ) I have a string value which has value like this: "[(1,2,3),(2,3),(6,8)]" -> string but I want to have these values in int type like this: [(1,2,3),...

Open file in ML(SMLNJ)

I need to read file in ML (SLMNJ) and save it in some structures. I need to read some data that points to graph declaration: [( 1 , 2 , 13 ),( 2 , 3 , 3 ),( 2 , 4 , 8 ),( 2 , 5 , 4 ),( 3 , 1 , 5 ),( ...

Line Comments in Standard ML

I m learning ML, with the SML/NJ dialect. What I m trying to figure out is if there is a line comment operator. I found the block comment operator, (* ... *), but I really miss line comments. ...

How to fix the SML/NJ interactive system to use Arrow Keys

I m having some trouble using SML/NJ interactive system, namely, that when I try to use my arrow keys (either left or right to make a correction in the expression I ve typed, up to repeat the last ...

What causes this Standard-ML type error?

I was trying to make a tail-recursive version of this very simple SML function: fun suffixes [] = [[]] | suffixes (x::xs) = (x::xs) :: suffixes xs; During the course of this, I was using type ...

热门标签