ocaml 该表达式的类型为“a list”,但此处与类型 X(account) 一起使用
有一段 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我不确定你到底想做什么,但我可以告诉你为什么它不起作用。
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 (typeaccount list
) but then you are doinghashtbl_fold.pos
; you cannot get apos
field of a list, hence the error.In
print_account
,acct.pos
works becauseacct
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?
您的回答(这不是帐户,而是帐户列表类型)帮助我弄清楚。
现在,我可以在 hashtbl_function 中使用 hash_tbl_fold.pos 。
这对我有用:
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:
Tnx!