使用模块Set Ocaml [英] Use module Set Ocaml

查看:80
本文介绍了使用模块Set Ocaml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个使用语法的程序,并查看该语法是否为LL(1).我想使用Set模块,但是我不知道该如何进行操作,当然,set元素的类型将为char,可以帮忙吗?

I am creating a program that uses a grammar and see if this grammar is LL (1). I want to use the module Set, but I have no idea how to proceed, of course, the type of the elements of the set will char, can you help?

推荐答案

此答案假定您已经知道如何确定语法是否为LL(1),并且仅在寻求有关Objective Caml特定用法的帮助. Set模块.

This answer assumes that you already know how to determine if a grammar is LL (1), and are merely looking for help on the specific usage of the Objective Caml Set module.

标准库Set提供了一个函子,您可以根据自己的特定需求来构建自己的set模块.您需要提供一个描述集合中元素类型的模块,以及一个与

The standard library Set provides a functor that lets you construct your own set module, adapted for your specific needs. You need to provide a module that describes the type of elements inside the set, and a comparison function that follows the same convention as compare : compare a b = 0 if a = b, compare a b < 0 if a < b and so on. For characters, this would be:

module OrderedChar = struct
  type t = char
  let compare = compare
end

module CharSet = Set.Make(OrderedChar)

上面的CharSet模块具有描述的界面在文档中.其要点是,集合是不可变的值(如列表),因此该模块为您提供了通过添加或删除元素从现有集合创建新集合的功能:

The CharSet module above has the interface described in the documentation. The gist of it is that sets are immutable values (like lists), so the module provides you with functions that create a new set from an existing set by adding or removing elements:

let a  = CharSet.add    'a' CharSet.empty 
let ab = CharSet.add    'b' a
let b  = CharSet.remove 'a' ab
(* /* a, b and ab are three sets containing respectively {a}, {b} and {ab} */ *)

对集合元素的访问主要通过存在查询和迭代进行:

Access to elements of a set happens mostly through existence queries and through iteration:

assert (CharSet.mem 'a' ab) 
assert (not (CharSet.mem 'c' b))

CharSet.iter print_char ab 
(* /* Prints 'ab' : follows the order defined by your 'compare' function */ *)

这篇关于使用模块Set Ocaml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆