如何进行模式匹配,执行一个功能,然后在执行的功能上进行模式匹配,以执行另一个功能 [英] how to pattern match, execute a function then pattern match on the executed function to execute another function
问题描述
我想编写一个函数,将数组中的元素从一个位置复制到另一个位置. copy_obj是执行此操作的函数.现在,我得到了一个表示要复制的元素位置的指针列表,因此,我需要在列表中的每个元素上应用函数copy_obj以及我应该开始复制的空闲位置的地址.在我的代码中是f.
I'm suppose to write a function that copies elements in a array from one place to the other. copy_obj is the function doing that. now I am given a list of pointers that represents the locations of elements i need to copy therefore I need to apply the function copy_obj on each elements in the list with the address of the free location where I should start to copy. In my code it is f.
考虑到函数copy_obj返回一对地址,并且其中一个是free的更新值,我需要使用它来递归地调用列表中其他元素上的函数.
Considering that the function copy_obj returns a pair of addresses, and one is the updated value of free, I need to use to call recursively the function on other elements on the list.
下面是我编写的代码,它可以编译,但我在| h::tl -> copy_obj f h
处警告说此表达式应具有类型单位"
below is the code I wrote, it compiles but I'am having a warning at | h::tl -> copy_obj f h
saying this "expression should have type unit"
我该怎么办?
let rec funct f myList =
match myList with
| [] -> f
| h::tl->
copy_obj f h;
match (copy_obj f h) with
| (free_n,addr_n) -> funct free_n tl
推荐答案
您似乎是另一位写Copy GC的学生. :-)
You seem to be another student writing Copy GC. :-)
表达式e1; e2
将按顺序执行e1
和e2
,通常e1
的返回类型应为unit
,这意味着不返回任何特殊内容".如果e1
是其类型不是unit
的表达式,则OCaml编译器会发出警告:表达式应具有类型单位",因为您可能会通过e1
计算出有意义的内容,但将其丢弃.有时这很好地表明了可能的错误.
Expression e1; e2
is to execute e1
and e2
sequentially, and normally e1
's return type is expected to be unit
, which means "returns nothing special". If e1
is an expression whose type is other than unit
, OCaml compiler emits a warning: "expression should have type unit", since it is possible that you compute something meaningful by e1
but you throw it away. It is sometimes a good indication of possible bugs.
在您的情况下,copy_obj f h
返回一个元组,可能是(int * int)
,而不是unit
.因此,您得到了警告.如果确实可以放弃计算结果,则必须写ignore (copy_obj f h)
,其中ignore : 'a -> unit
.
In your case, copy_obj f h
returns a tuple, probably (int * int)
, and it is not unit
. Therefore you got the warning. If you really ok to discard the compuation result you must write ignore (copy_obj f h)
, where ignore : 'a -> unit
.
您两次致电copy_obj f h
,这似乎很奇怪.如果没有该函数的定义,我们将无法100%确定,但我想您不需要第一次调用:
You called copy_obj f h
twice, which seems very strange. Without the definition of the function we cannot tell 100% sure but I guess you do not need the first call:
let rec funct f myList =
match myList with
| [] -> f
| h::tl->
match copy_obj f h with
| (free_n,addr_n) -> funct free_n tl
如果要实现Copy GC,并且copy_obj
将对象h
复制到可用插槽中且有副作用的可用插槽中,则在此调用两次函数将h
存储两次.对于GC算法,这将是一个严重的错误.实际上,警告实际上会尽力为您提供帮助!!
If you are implementing Copy GC and if copy_obj
copies an object h
to a free slot somewhere available using side effect, calling the function twice here stores h
twice. It would be a serious bug for a GC algorithm. And the warning actually tries to help you at this exact point!!
还有一件事. match
不是解构您价值的唯一方法. let
还可以提取元组的元素.通常我们这样写:
One more thing. match
is not the only way to deconstruct your value. let
can also extract the elements of your tuple. Normally we write like:
let rec funct f myList =
match myList with
| [] -> f
| h::tl->
let (free_n, addr_n) = copy_obj f h in
funct free_n tl
或者您也可以简单地写funct (fst (copy_obj f h)) tl
.
Or you can simply write funct (fst (copy_obj f h)) tl
.
这篇关于如何进行模式匹配,执行一个功能,然后在执行的功能上进行模式匹配,以执行另一个功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!