English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Re: 'a Set?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-01-26 (14:07)
From: Jeff Shaw <shawjef3@m...>
Subject: Re: 'a Set?
Mike,
I know of no way to do what you're asking for. However, you can always use lists as an ad hoc kind of set. Use List.mem to check membership, and some functions to add elements or create a set while looking for duplicates:


    let add n list =
      let rec aux = function
   l::ls when compare l n = 0 -> list
 | l::ls -> aux ls
 | [] -> list @ [n]
      in aux list

    let create list =
      let rec aux accumulator = function
   l::ls -> aux (add l accumulator) ls
 | [] -> accumulator
      in aux [] list

This is terribly inefficient for big sets, but it works. It could be made more efficient by making sure the lists are ordered, etc.

Jeff