Print certain lines only if it is not an empty string

六月ゝ 毕业季﹏ 提交于 2020-06-01 07:41:31

问题


Assume that

type house = {name: string; nb_windows: int; nb_doors: int; nb_cars: int; nb_rooms: int};;
house1 = {name="house1", nb_windows = 10; nb_doors = 50; nb_cars = 3; nb_rooms= 10};;

I want to print each element of record house1.

Name : house1
Number of windows : 10
Number of doors : 50
Number of cars : 3
Number of rooms : 10

A way I have define the function is

let test (lch: house) = 
    begin
      print_string ("Name : " ^ lch.name ^ "\n");
      print_string ("Number of windows : " ^ string_of_int lch.nb_windows ^ "\n");
      print_string ("Number of doors : " ^ string_of_int lch.nb_doors ^ "\n");
      print_string ("Number of car : " ^ string_of_int lch.nb_cars ^ "\n");
      print_string ("Number of rooms : " ^ string_of_int lch.nb_rooms ^ "\n\n");
    end;;

test house1;;

However, that function is very limited. For instance, if I define

house2 = {name=""; nb_windows=10; nb_doors = 50; nb_cars = 3; nb_rooms= 10}

In that example, I don't want to print Name : and Number of windows : as it is either an empty string or the key is simply not defined.

How can I modify the test function so that the line test house2;; will display only

Number of windows : 10
Number of doors : 50
Number of cars : 3
Number of rooms : 10

instead of

Name :
Number of windows : 10
Number of doors : 50
Number of cars : 3
Number of rooms : 10

Be aware that I have to use only the functional paradigm, so no while or for loops.

UPDATE

It seems it is possible to do it that way, but I am not sure. Can someone provide an example how to use it?

来源:https://stackoverflow.com/questions/62119842/print-certain-lines-only-if-it-is-not-an-empty-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!