Does Common Lisp have a something like java's Set Interface/implementing classes?

前端 未结 8 1040
无人及你
无人及你 2021-01-12 01:58

I need something like this, a collection of elements which contains no duplicates of any element. Does Common Lisp, specifically SBCL, have any thing like this?

相关标签:
8条回答
  • 2021-01-12 02:36

    For a quick solution, just use hash tables, as has been mentioned before.

    However, if you prefer a more principled approach, you can take a look at FSet, which is “a functional set-theoretic collections library”. Among others, it contains classes and operations for sets and bags.

    (EDIT:) The cleanest way would probably be to define your set-oriented operations as generic functions. A set of generic functions is basically equivalent to a Java interface, after all. You can simply implement methods on the standard HASH-TABLE class as a first prototype and allow other implementations as well.

    0 讨论(0)
  • 2021-01-12 02:37

    Not that I'm aware of, but you can use hash tables for something quite similar.

    0 讨论(0)
  • 2021-01-12 02:39

    Yes, it has sets. See this section on "Sets" from Practical Common Lisp.

    Basically, you can create a set with pushnew and adjoin, query it with member, member-if and member-if-not, and combine it with other sets with functions like intersection, union, set-difference, set-exclusive-or and subsetp.

    0 讨论(0)
  • 2021-01-12 02:42

    Lisp hashtables are CLOS based. Specs here.

    0 讨论(0)
  • 2021-01-12 02:42

    Personally, I would just implement a function which takes a list and return a unique set. I've drafted something together which works for me:

    (defun make-set (list-in &optional (list-out '()))
      (if (endp list-in)
          (nreverse list-out)
          (make-set
            (cdr list-in)
            (adjoin (car list-in) list-out :test 'equal))))
    

    Basically, the adjoin function prepends an item to a list non-destructively if and only if the item is not already present in the list, accepting an optional test function (one of the Common Lisp "equal" functions). You can also use pushnew to do so destructively, but I find the tail-recursive implementation to be far more elegant. So, Lisp does export several basic functions that allow you to use a list as a set; no built-in datatype is needed because you can just use different functions for prepending things to a list.

    My data source for all of this (not the function, but the info) has been a combination of the Common Lisp HyperSpec and Common Lisp the Language (2nd Edition).

    0 讨论(0)
  • 2021-01-12 02:43

    Look at cl-containers. There is a set-container class.

    0 讨论(0)
提交回复
热议问题