简体   繁体   English

OCaml 中类型 ('a -> 'b) list -> 'a -> 'b list 的函数

[英]Function of type ('a -> 'b) list -> 'a -> 'b list in OCaml

Write any Ocaml function whose type is ('a -> 'b) list -> 'a -> 'b list编写任何类型为('a -> 'b) list -> 'a -> 'b list Ocaml 函数

('a -> 'b) list is the part that confuses me the most. ('a -> 'b) list是最让我困惑的部分。 I'm new to OCaml and having a hard time understanding how to write a function to get a specific datatype type.我是 OCaml 的新手,很难理解如何编写函数来获取特定的数据类型。

# let int x = x+1;;
# let fcn = [int; int];;

So I'm passing a function a function and a variable.所以我传递了一个函数、一个函数和一个变量。 I'm going to take that variable an add it to each element of the list and return the list?我要将该变量添加到列表的每个元素并返回列表?

('a -> 'b) means a function which goes from type 'a to type 'b . ('a -> 'b)表示从类型'a到类型'b的函数。 Basically you need to make a function which takes a list of functions that take 'a and return 'b , plus a specific 'a value, and which returns a list of 'b values (probably by applying each function of the list of functions to the specific 'a value).基本上你需要创建一个函数,它接受一个函数列表,这些函数接受'a并返回'b ,加上一个特定的'a值,并返回一个'b值列表(可能通过将函数列表的每个函数应用到特定的'a值)。

As this is homework, I will not provide you with a complete solution.由于这是作业,我不会为您提供完整的解决方案。 But, as a hint, I would suggest that you take a look at this implementation of the familiar map function:但是,作为提示,我建议您看一下熟悉的map函数的这个实现:

let rec map f = function
  | [] -> []
  | x :: xs -> f x :: map f xs

It has type ('a -> 'b) -> 'a list -> 'b list which means that it takes as its first argument a function that takes values of some type 'a to values of some type 'b , as its second argument a list of elements of type 'a , and that it produces a list of elements of type 'b .它有类型('a -> 'b) -> 'a list -> 'b list这意味着它需要一个函数作为它的第一个参数,该函数将某种类型'a'a的值转换为某种类型'b'b ,作为它的第二个参数是一个类型为'a的元素列表,它产生一个类型为'b的元素列表。 It proceeds by pattern matching on the argument list and, recursively applying the function ( f ) to every element x of the list.它通过对参数列表进行模式匹配,然后将函数 ( f ) 递归地应用于列表的每个元素x

Now have a look at the type of the function that you have to write?现在看看你要写的函数的类型? What does it tell you about the required behaviour of that function?它告诉您有关该功能所需行为的什么信息? Keeping the implementation of the map function in mind, how would you write your function?牢记map函数的实现,您将如何编写您的函数?

('a -> 'b) list -> 'a -> 'b list

This means that your function has two parameters这意味着您的函数有两个参数

  • A list of ('a -> 'b) which represents a function taking an element of type 'a as a parameter and returning an element of type 'b .一个('a -> 'b)列表,它表示一个函数,将一个类型为'a的元素作为参数并返回一个类型为'b的元素。 As you can see, these types are abstract, so they could be of any types for instance (int -> int) or (int -> float) etc...如您所见,这些类型是抽象的,因此它们可以是任何类型,例如(int -> int)(int -> float)等...
  • An elements of types 'a . 'a类型的元素。 Notice that this type must be the same as the parameter of your function.请注意,此类型必须与您的函数的参数相同。

So you'll build the resulting list with the element you give as a parameter.因此,您将使用您提供的元素作为参数来构建结果列表。

Here is a little example:这是一个小例子:

let action l a =
  let rec todo l res =
    match l with
      | [] -> res
      | h :: t -> todo t res@[h a] in
  todo l []

so here, any function of type int -> int will be accepted.所以在这里,任何类型为int -> int函数都将被接受。 The same thing goes for any other type as long as you don't mix them with other types.只要不将它们与其他类型混合,任何其他类型也是如此。

let rec func f a = match f with          (* ( 'a->'b ) list -> 'a -> 'b list *)
                       |[]->[]
                       |x::lr -> x a :: func lr a;; 

that may help !这可能有帮助! it works fine它工作正常
1 - So as we know , ocaml create the type of our function line by line 1 -正如我们所知,ocaml 逐行创建我们的函数类型

2 - in this function we have two arguments f and a 2 -在这个函数中我们有两个参数fa

3 - ( 'a->'b ) list : for f 3 - ( 'a->'b ) 列表:对于f

4 - 'a : for a 4 - 'a : 对于一个
! how ocaml did that ? ocaml 是怎么做到的? listen !听 !

5 - when we matched f with [ ] 'blank list' ocaml release that is a list (****)list but doesn't know what contains yet in the last line of the code he will do ok ? 5 -当我们将 f 与 [] 'blank list' ocaml 版本匹配时,它是一个列表(****) 列表但不知道代码的最后一行中包含什么,他会做吗? nice !好的 !
- here we are in the last line of the code and we have only f of type list - - 这里我们在代码的最后一行,我们只有 f 类型的列表 -

6 - x :: lr means we sort the first element of the element that is matched before : f and we add a here ocaml gives a type for a and for the list elements which is matched : f as first elements ocaml gives them types from 'a to 'z so here we have ('a->'b) list for f and 'a for a 6 - x :: lr意味着我们对之前匹配的元素的第一个元素进行排序 : f并且我们此处添加一个ocaml 为a和匹配的列表元素提供类型: f作为第一个元素'一个'Z所以在这里,我们有(' A - >' b)名单f“一
-here we have f of type : ('a->'b) list , and a of type : 'a - 这里我们有f类型: ('a->'b) list ,和a类型: 'a

7 - the result of this function 'b list so it's up to you to answer in comment ! 7 -此函数的结果'b list所以由您在评论中回答! :D thank you :D 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM