只有当它不是空字符串时才打印
Printing only when it is not an empty string
假设
type house = {first_name: string; last_name: string; nb_windows: int; nb_doors: int; nb_cars: int; nb_rooms: int};;
house1 = {first_name="John"; last_name="Doe; nb_windows = 10; nb_doors = 50; nb_cars = 3; nb_rooms= 10};;
我想打印记录的每个元素house1
。
Full Name : John Doe
Number of windows : 10
Number of doors : 50
Number of cars : 3
Number of rooms : 10
我定义函数的一种方式是
let test (lch: house) =
begin
print_string ("Full Name : " ^ lch.first_name ^ " " ^ lch.last_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;;
但是,该功能非常有限。例如,如果我定义
house2 = {first_name=""; last_name="" nb_windows=10; nb_doors = 50; nb_cars = 3; nb_rooms= 10}
在那个例子中,如果 first_name
和 last_name
是空字符串,我不想打印 Full Name
。
如何修改 test
函数,使行 test house2;;
只显示
Number of windows : 10
Number of doors : 50
Number of cars : 3
Number of rooms : 10
而不是
Full Name :
Number of windows : 10
Number of doors : 50
Number of cars : 3
Number of rooms : 10
请注意,我必须仅使用功能范例,因此没有 while
或 for
循环。
回答开始
我想我可以使用像
这样的东西
let print_data x y = match x with
| "" -> ()
| x -> print_string y;;
但我不知道如何继续
您的函数 print_data
测试一个字符串以确定是否打印另一个字符串。如果将 lch.first_name ^ lch.last_name
作为测试字符串传递,并将所需的输出字符串作为第二个字符串传递,则可以使用此函数。
具体来说,替换这一行:
print_string ("Full Name : " ^ lch.first_name ^ " " ^ lch.last_name "\n");
用这个代替:
print_data (lch.first_name ^ lch.last_name)
("Full Name : " ^ lch.first_name ^ " " ^ lch.last_name "\n");
就其价值而言,这只是编程,与 OCaml 没有太大关系(恕我直言)。
作为一些不请自来的建议:如果你的问题是你无法查看 print_data
并弄清楚它是如何工作的,那么要做的就是阅读一两个 OCaml 教程,直到你能够理解语言更容易。这比在 Whosebug 上单独提问要快。
更新
这是一个仅格式化给定的非空字符串的函数:
let concat_non_null strings =
String.concat " " (List.filter ((<>) "") strings)
打印姓名:
match concat_non_null [lch.first_name; lch.last_name] with
| "" -> ()
| s -> print_endline ("Full_name: " ^ s)
你可能会争辩说,这可以很好地推广到更多的名字——比如当还有一个中间名时。
let print_data a b =
let has_empty_string list_strings = List.exists (fun x -> x = "") list_strings in
match a with
| [] -> ()
| _ -> if not (has_empty_string a) then print_string b else ();;
print_data [lch.first_name, lch.last_name] ("Full Name : " ^ lch.first_name ^ " " ^ lch.last_name "\n");;
这行得通,但是 lch.first_name
和 lch.last_name
重复了两次,这有点烦人。
假设
type house = {first_name: string; last_name: string; nb_windows: int; nb_doors: int; nb_cars: int; nb_rooms: int};;
house1 = {first_name="John"; last_name="Doe; nb_windows = 10; nb_doors = 50; nb_cars = 3; nb_rooms= 10};;
我想打印记录的每个元素house1
。
Full Name : John Doe
Number of windows : 10
Number of doors : 50
Number of cars : 3
Number of rooms : 10
我定义函数的一种方式是
let test (lch: house) =
begin
print_string ("Full Name : " ^ lch.first_name ^ " " ^ lch.last_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;;
但是,该功能非常有限。例如,如果我定义
house2 = {first_name=""; last_name="" nb_windows=10; nb_doors = 50; nb_cars = 3; nb_rooms= 10}
在那个例子中,如果 first_name
和 last_name
是空字符串,我不想打印 Full Name
。
如何修改 test
函数,使行 test house2;;
只显示
Number of windows : 10
Number of doors : 50
Number of cars : 3
Number of rooms : 10
而不是
Full Name :
Number of windows : 10
Number of doors : 50
Number of cars : 3
Number of rooms : 10
请注意,我必须仅使用功能范例,因此没有 while
或 for
循环。
回答开始
我想我可以使用像
这样的东西let print_data x y = match x with
| "" -> ()
| x -> print_string y;;
但我不知道如何继续
您的函数 print_data
测试一个字符串以确定是否打印另一个字符串。如果将 lch.first_name ^ lch.last_name
作为测试字符串传递,并将所需的输出字符串作为第二个字符串传递,则可以使用此函数。
具体来说,替换这一行:
print_string ("Full Name : " ^ lch.first_name ^ " " ^ lch.last_name "\n");
用这个代替:
print_data (lch.first_name ^ lch.last_name)
("Full Name : " ^ lch.first_name ^ " " ^ lch.last_name "\n");
就其价值而言,这只是编程,与 OCaml 没有太大关系(恕我直言)。
作为一些不请自来的建议:如果你的问题是你无法查看 print_data
并弄清楚它是如何工作的,那么要做的就是阅读一两个 OCaml 教程,直到你能够理解语言更容易。这比在 Whosebug 上单独提问要快。
更新
这是一个仅格式化给定的非空字符串的函数:
let concat_non_null strings =
String.concat " " (List.filter ((<>) "") strings)
打印姓名:
match concat_non_null [lch.first_name; lch.last_name] with
| "" -> ()
| s -> print_endline ("Full_name: " ^ s)
你可能会争辩说,这可以很好地推广到更多的名字——比如当还有一个中间名时。
let print_data a b =
let has_empty_string list_strings = List.exists (fun x -> x = "") list_strings in
match a with
| [] -> ()
| _ -> if not (has_empty_string a) then print_string b else ();;
print_data [lch.first_name, lch.last_name] ("Full Name : " ^ lch.first_name ^ " " ^ lch.last_name "\n");;
这行得通,但是 lch.first_name
和 lch.last_name
重复了两次,这有点烦人。