arguments - 如何在 sml 中将柯里化函数的输入声明为实数?
问题描述
这是我想要输出真实的咖喱阶乘函数的代码
fun pow(x:real) (n:real)= if (n=0.0) then 1.0 else x:real*pow(x:real) (n-1:real) ;
但是我的语法真的是错误的,我该如何解决这个问题?
解决方案
我想你想要的是:
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 ;他们不添加任何东西,因为编译器可以推断类型。
推荐阅读
- ios - In SwiftUI is there a way to create a state variable that is bound to whether or not the view being defined is the first responder?
- pymatgen - Download data with topological classification
- rest - "TkPointer not currently supported" error in Delphi 10.1 when passing a populated TObjectList in REST Datasnap
- google-apps-script - How to add a table of contents to a Google Sheets spreadsheet?
- c++ - boost::variant 中持有的类的复制构造函数的问题
- swift - 如何以编程方式在 Swift 中扩展 NSPopUpButton?
- bash - UNIX Bash - 从文件中的特定字符串中删除双引号
- python - 如何在 python 中使用条件来解决下面显示的代码?
- c++ - Curl with vcpkg
- openiddict - Invalid Cast Exception with on .NET Core 3.1 with 3.0.0-beta1.20306.64