首页 > 解决方案 > 如何在 sml 中将柯里化函数的输入声明为实数?

问题描述

这是我想要输出真实的咖喱阶乘函数的代码

fun pow(x:real) (n:real)= if (n=0.0) then 1.0 else x:real*pow(x:real) (n-1:real) ;

但是我的语法真的是错误的,我该如何解决这个问题?

标签: argumentssmlcurryingsmlnjml

解决方案


我想你想要的是:

fun pow x n =
  if n = 0
  then 1.0
  else x * pow x (n - 1)

或者,如果您想更明确地了解类型:

fun pow (x : real) (n : int) : real =
  if n = 0
  then 1.0
  else x * pow x (n - 1)

那是:

  • 我认为你想n成为类型int,而不是类型real。(你的方法只有在是一个非负整数时才有意义n,否则递归将永远持续下去。)
  • 你不需要:real到处都有那么多 -s ;他们不添加任何东西,因为编译器可以推断类型。

推荐阅读