如何在main.ml文件中使用此词典(创建词典,添加元素,删除....)?

时间:2019-05-01 19:33:34

标签: dictionary ocaml

我找到了字典和您的实现的实现。我想使用下面的那些模块创建字典,但出现此错误:

  

未绑定值。

我是功能范式的新手。我尝试过这本书,但仍然迷路:http://dev.realworldocaml.org/maps-and-hashtables.html

然后 字典和AssociateList从这里:https://www.cs.cornell.edu/courses/cs3110/2014sp/recitations/7/functional-stacks-queues-dictionaries-fractions.html

let x = Dictionary.AssocList.create ;;

open Dictionary.AssocList
enviroment = create() 
Unbound type constructor stringAssocListAssocList

module type DICTIONARY =
  sig
    (* An 'a dict is a mapping from strings to 'a.                                                                                                                                
       We write {k1->v1, k2->v2, ...} for the dictionary which                                                                                                                    
       maps k1 to v1, k2 to v2, and so forth. *)
    type key = string
    type 'a dict

    (* make an empty dictionary carrying 'a values *)
    val make : unit -> 'a dict

    (* insert a key and value into the dictionary *)
    val insert : 'a dict -> key -> 'a -> 'a dict

    (* Return the value that a key maps to in the dictionary.                                                                                                                     
     * Raise NotFound if there is not mapping for the key. *)
    val lookup : 'a dict -> key -> 'a
    exception NotFound

    (* applies a function to all the elements of a dictionary;                                                                                                                    
       i.e., if a dictionary d maps a string s to an element a,                                                                                                                   
       then the dictionary (map f d) will map s to f(a). *)
    val map : ('a -> 'b) -> 'a dict -> 'b dict
  end

module AssocList : DICTIONARY =
  struct
    type key = string
    type 'a dict = (key * 'a) list

    (* AF: The list [(k1,v1), (k2,v2), ...] represents the dictionary                                                                                                             
     * {k1 -> v1, k2 -> v2, ...}, except that if a key occurs                                                                                                                     
     * multiple times in the list, only the earliest one matters.                                                                                                                 
     * RI: true.                                                                                                                                                                  
     *)

    let make() : 'a dict = []

    let insert (d : 'a dict) (k : key) (x : 'a) : 'a dict =
      (k, x) :: d

    exception NotFound

    let rec lookup (d : 'a dict) (k : key) : 'a =
      match d with
      | [] -> raise NotFound
      | (k', x) :: rest ->
         if k = k' then x
         else lookup rest k
    let map (f : 'a -> 'b) (d : 'a dict) =
      List.map (fun (k, a) -> (k, f a)) d
  end

1 个答案:

答案 0 :(得分:0)

假设您的字典实现保存在名为dictionary.ml的文件中。要使用此模块,您可以open,并为AssocList提供一个简短的名称,以方便使用(可选)

open Dictionary
module D = AssocList

要创建一个新的空字典,您将执行以下操作:

let dict = D.make ()

要插入元素并由此做出新的决定,您将执行以下操作:

let new_dict = D.insert dict "one" 1

"one"是键,1是值。

如果要查看字典中的所有元素,则必须提供一个新功能或使(key * 'a) list类型可用,在签名中像这样

type 'a dict = (key * 'a) list (* This will make dict elements available *)
type 'a dict (* This will make the dict available but you can't view the elements *)