我有一个 Ocaml 函数的问题,它接受两个输入并且应该给出一个特定的输出
I am having an issue with an Ocaml function that takes in two inputs and is supposed ti give a specific output
我正在处理家庭作业问题,通常我知道为什么会出现错误,但是,对于这两个问题,我不知道发生了什么。我不确定是模式匹配问题还是模式匹配后我试图解决问题的方式。
Given a STRING LIST and INT return a STRING LIST
that contains all the strings with a length greater than the given int
let rec long_strings (l: string list) (i: int) : string list =
match l with
| [] -> []
| head :: tail -> if (String.length head > i)
then head :: (long_strings tail)
else (long_strings tail)
错误
This expression has type int -> string list
but an expression was expected of type string list
另一个类似的问题...
(*Given an INT LIST and INT divide the numbers in the given int
list by the given int and return an (INT * INT) LIST containing
the dividend and the remainder*)
let rec remainders (l: int list) (i: int) : (int * int) list =
match l with
| [] -> []
| head :: tail -> ((head / i), (head mod num)) :: (remainders tail)
;;
错误
This expression has type int -> (int * int) list
but an expression was expected of type (int * int) list
您缺少一个参数。 long_strings
的类型是 string list -> int -> string list
。因此,如果您将 long_strings
应用于字符串列表,您将获得一个函数:int -> string list
。
例如,可以定义
let list = ["1"; "12"; "123"]
let filter_list = long_strings list
let _ = assert ( filter_list 0 = ["1"; "12"; "123"] )
let _ = assert ( filter_list 2 = ["123"] )
然而,在
head :: long_strings tail
您期望 long_strings tail
是一个列表。
因此错误消息
This expression has type int -> string list
but an expression was expected of type string list
告诉你 long_strings tail
不是一个列表,而是一个接受 int
和 returns 和 string_list
.
的函数
换句话说,您忘记了 i
参数。
我正在处理家庭作业问题,通常我知道为什么会出现错误,但是,对于这两个问题,我不知道发生了什么。我不确定是模式匹配问题还是模式匹配后我试图解决问题的方式。
Given a STRING LIST and INT return a STRING LIST
that contains all the strings with a length greater than the given int
let rec long_strings (l: string list) (i: int) : string list =
match l with
| [] -> []
| head :: tail -> if (String.length head > i)
then head :: (long_strings tail)
else (long_strings tail)
错误
This expression has type int -> string list
but an expression was expected of type string list
另一个类似的问题...
(*Given an INT LIST and INT divide the numbers in the given int
list by the given int and return an (INT * INT) LIST containing
the dividend and the remainder*)
let rec remainders (l: int list) (i: int) : (int * int) list =
match l with
| [] -> []
| head :: tail -> ((head / i), (head mod num)) :: (remainders tail)
;;
错误
This expression has type int -> (int * int) list
but an expression was expected of type (int * int) list
您缺少一个参数。 long_strings
的类型是 string list -> int -> string list
。因此,如果您将 long_strings
应用于字符串列表,您将获得一个函数:int -> string list
。
例如,可以定义
let list = ["1"; "12"; "123"]
let filter_list = long_strings list
let _ = assert ( filter_list 0 = ["1"; "12"; "123"] )
let _ = assert ( filter_list 2 = ["123"] )
然而,在
head :: long_strings tail
您期望 long_strings tail
是一个列表。
因此错误消息
This expression has type int -> string list but an expression was expected of type string list
告诉你 long_strings tail
不是一个列表,而是一个接受 int
和 returns 和 string_list
.
换句话说,您忘记了 i
参数。