OCaml 三重整数列表中的每个数字

伊姆达

所以我需要编写一个函数,它将整数列表中的每个数字增加三倍

这是我到目前为止所拥有的:

let number =  [1; 2; 3; 4];;

let rec print_list_int myList = match myList with
| [] -> print_endline "This is the end of the int list!"
| head::body -> 
begin
print_int head * 3; 
print_endline "";
print_list_int body *3
end
;;

print_list_int number;; 

它似乎没有做任何有用的事情,我哪里出错了?需要它输出,但它也没有这样做。提前致谢!:)

杰弗里·斯科菲尔德

这个表达:

print_int head * 3

解释如下:

(print_int head) * 3

因为函数调用(应用程序)具有很高的优先级。你需要像这样括号:

print_int (head * 3)

下面的类似情况是一个不同的问题:(print_list_int body) * 3没有意义但print_list_int (body * 3)也没有意义。您不能将列表乘以 3。但是,您不需要在此调用中乘以。print_list_int函数将(递归地)为您进行乘法运算。

更新

如果我进行了上面暗示的更改,我会在 OCaml 顶层看到这一点:

val print_list_int : int list -> unit = <fun>
# print_list_int number;;
3
6
9
12
This is the end of the int list!
- : unit = ()
#

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章