3

I am going to practicing using Map in Ocaml.

I found that the usage of Map is quite different from List, Array, etc.

I understand it is applying functor which I haven't learnt yet. but it is fine.


Here is my IntMap

module IntMap = Map.Make(struct type t = int let compare = compare end)

So, now I can use IntMap to add by IntMap.add x y map, etc, right?


I have a few questions:

  1. How do I control the type of value in the map?
  2. If I want a alias type for my IntMap, what should I do? I can do type 'a my_type = 'a list, but how to do for map?
  3. I find that IntMap is like List and both of them are actually modules. But List has a type of list, what about the map?
4

1 に答える 1

3

Q:

マップ内の値のタイプを制御するにはどうすればよいですか?

必要はありません'a IntMap.tが、 type の値を含むパラメーター化された型'aです。IntMapしたがって、int から int へ、int から bool へ、int から関数へのマップに1 つのモジュールを使用できます (もちろん、1 つのマップ値には 1 つの型のバインディングのみを含めることができます)。さらに、以外の型であることを強制するIntMap.empty理由がないのと同様に、の型を制約しようとする理由はありません。[]'a list

Q:

IntMap のエイリアス タイプが必要な場合はどうすればよいですか? type 'a my_type = 'a list はできますが、map の場合はどうすればよいですか?

こちらです:

type 'a imap = 'a IntMap.t

Q:

IntMap は List に似ており、どちらも実際にはモジュールであることがわかりました。リストにはリストの型がありますが、マップはどうでしょうか。

これはIntmap.t

于 2013-04-04T11:59:52.267 に答える