在OCaml中打印列表 [英] Print a List in OCaml

查看:82
本文介绍了在OCaml中打印列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做些简单的事情:

打印列表.

let a = [1;2;3;4;5]

如何将此列表打印到标准输出"?

How can I print this list to Standard Output?

推荐答案

您可以通过简单的递归来实现:

You can do this with a simple recursion :

let rec print_list = function 
[] -> ()
| e::l -> print_int e ; print_string " " ; print_list l

将打印列表的开头,然后在列表的末尾进行递归调用.

The head of the list is printed, then you do a recursive call on the tail of the list.

这篇关于在OCaml中打印列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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