ocaml 该表达式的类型为“a list”,但此处与类型 X(account) 一起使用

发布于 2024-11-08 15:25:41 字数 1290 浏览 0 评论 0原文

有一段 ocaml 可以正常工作

type position = {symbol: string; holding: int; pprice : float; };;
type account  = {name: string; max_ind_holding:float; pos:position list};;

let print_position pos = print_string "Holding: "; print_int pos.holding;
print_string ( " " ^ pos.symbol ^ "@" );
print_float pos.pprice;
print_newline(  );;


let print_account acct = print_string ( "Account_ID " ^ acct.name );
print_newline(  ); 
List.iter print_position acct.pos;;


(*MAIN PART OF CODE *)

let hashtbl_function db = 
    let hash_tbl_fold = Hashtbl.fold ( fun x y z -> ( ( y ) :: z ) ) db [] in
    ( fun x -> List.iter print_account x ) hash_tbl_fold (* THIS WORKS FINE *)
;;

但是,如果我想更改最后一个,例如在此函数中(不在 print_account 中)迭代“位置列表”,我会收到错误:

let hashtbl_function db = 
    let hash_tbl_fold = Hashtbl.fold ( fun x y z -> ( ( y ) :: z ) ) db [] in
    ( fun x -> List.iter print_position x ) hash_tbl_fold.pos (* Why this doesn't work ???*)
;;

错误:此表达式的类型为“a” list 但在这里与 account 类型一起使用

但是 hash_tbl_fold.pos 是位置列表,这不是帐户类型。正确的?

在 print_account 中 - acct.pos (获取“列表”)是可行的,但在 hashtbl_function 中, hash_tbl_fold.pos ->不是。

为什么同一个表达式只能在其中一个函数中起作用? 我如何迭代 hashtbl_function 中的“位置列表”?

提前致谢!

there is piece of ocaml which works correctly

type position = {symbol: string; holding: int; pprice : float; };;
type account  = {name: string; max_ind_holding:float; pos:position list};;

let print_position pos = print_string "Holding: "; print_int pos.holding;
print_string ( " " ^ pos.symbol ^ "@" );
print_float pos.pprice;
print_newline(  );;


let print_account acct = print_string ( "Account_ID " ^ acct.name );
print_newline(  ); 
List.iter print_position acct.pos;;


(*MAIN PART OF CODE *)

let hashtbl_function db = 
    let hash_tbl_fold = Hashtbl.fold ( fun x y z -> ( ( y ) :: z ) ) db [] in
    ( fun x -> List.iter print_account x ) hash_tbl_fold (* THIS WORKS FINE *)
;;

But, if i want to change last like to do iteration of 'position list' in this function (not in print_account), i will got error :

let hashtbl_function db = 
    let hash_tbl_fold = Hashtbl.fold ( fun x y z -> ( ( y ) :: z ) ) db [] in
    ( fun x -> List.iter print_position x ) hash_tbl_fold.pos (* Why this doesn't work ???*)
;;

Error: This expression has type 'a list but is here used with type account

But hash_tbl_fold.pos is list of position, this is not account type. Right?

In print_account - acct.pos (to get 'a list) is work, but in hashtbl_function, hash_tbl_fold.pos -> not.

How it can be, that the same expression only works in one of the functions ?
How i can iterate 'position list' in hashtbl_function?

thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绾颜 2024-11-15 15:25:41

好吧,我不确定你到底想做什么,但我可以告诉你为什么它不起作用。
hashtbl_fold 为您提供帐户列表(输入 account list),但您正在执行 hashtbl_fold.pos;您无法获取列表的 pos 字段,因此会出现错误。

print_account 中,acct.pos 可以工作,因为 acct 是单个帐户;不是帐户列表。

你想让我做什么?打印所有账户的所有仓位?或者只是单个(哪个?)账户的头寸?

Well, I'm not sure what exactly you want to do but I can tell you why it doesn't work.
hashtbl_fold gives you a list of accounts (type account list) but then you are doing hashtbl_fold.pos; you cannot get a pos field of a list, hence the error.

In print_account, acct.pos works because acct is a single account; not a list of accounts.

What do you want to do? Print all positions of all accounts? Or just positions of a single (which?) account?

无人问我粥可暖 2024-11-15 15:25:41

您的回答(这不是帐户,而是帐户列表类型)帮助我弄清楚。
现在,我可以在 hashtbl_function 中使用 hash_tbl_fold.pos 。
这对我有用:

let hash_tbl_fold =  Hashtbl.fold ( fun x y z -> ( ( y ) :: z ) ) db [] in
List.iter ( fun x -> List.iter print_position x.pos ) hash_tbl_fold

Tnx!

Your answer (this is not account, but account list type) help me to figure it out.
Now, i can use hash_tbl_fold.pos inside my hashtbl_function.
This works for me:

let hash_tbl_fold =  Hashtbl.fold ( fun x y z -> ( ( y ) :: z ) ) db [] in
List.iter ( fun x -> List.iter print_position x.pos ) hash_tbl_fold

Tnx!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文