-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
223 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
Bags | ||
==== | ||
|
||
For any :doc:`type <types>` ``T``, ``Bag(T)`` is the type of *finite | ||
bags* with elements of type ``T``. A bag is like a :doc:`set` (and | ||
unlike a :doc:`list`) in that it doesn't care about the order of the | ||
elements; however, unlike a :doc:`set`, a bag cares *how many copies* | ||
of each element there are. | ||
|
||
* The built-in ``bag`` function can be used to convert other | ||
collections (*e.g.* :doc:`sets <set>` or :doc:`lists <list>`) to bags. | ||
|
||
:: | ||
|
||
Disco> bag([]) | ||
⟅⟆ | ||
Disco> bag({1,2,3}) | ||
⟅1, 2, 3⟆ | ||
Disco> bag("hello") | ||
⟅'e', 'h', 'l' # 2, 'o'⟆ | ||
|
||
* Notice how single elements are simply listed by themselves, but | ||
elements occuring more than once are written with a ``#`` followed | ||
by a natural number. You can write bags this way yourself: | ||
|
||
:: | ||
|
||
Disco> ⟅'a' # 2, 'b' # 3⟆ | ||
⟅'a' # 2, 'b' # 3⟆ | ||
Disco> ⟅'a' # 2, 'b' # 1⟆ | ||
⟅'a' # 2, 'b'⟆ | ||
Disco> ⟅'a' # 0, 'b' # 1⟆ | ||
⟅'b'⟆ | ||
|
||
* Of course, entering the ``⟅⟆`` characters can be difficult, so you | ||
can also just stick with entering bags via the ``bag`` function. | ||
|
||
* One common place where bags show up is in the output of the ``factor`` | ||
function (from the ``num`` standard library module) for representing | ||
the prime factorization of a natural number. In a prime | ||
factorization, the *order* of the prime factors does not matter (since | ||
multiplication is commutative), but it matters how many copies there | ||
are of each factor. | ||
|
||
:: | ||
|
||
Disco> import num | ||
Disco> factor(10) | ||
⟅2, 5⟆ | ||
Disco> factor(12) | ||
⟅2 # 2, 3⟆ | ||
Disco> factor(64) | ||
⟅2 # 6⟆ | ||
|
||
* Just as with :doc:`lists <list>` and :doc:`sets <set>`, an | ||
:doc:`ellipsis <ellipsis>` can be used to generate a range of | ||
elements in a bag. For example, | ||
|
||
:: | ||
|
||
Disco> ⟅'a' .. 'e'⟆ | ||
⟅'a', 'b', 'c', 'd', 'e'⟆ | ||
Disco> ⟅1, 3 .. 9⟆ | ||
⟅1, 3, 5, 7, 9⟆ | ||
|
||
* :doc:`Comprehension <comprehension>` notation can also be used, | ||
for example: | ||
|
||
:: | ||
|
||
Disco> ⟅ abs(x - 5) | x in ⟅1 .. 10⟆, x^2 > 5 ⟆ | ||
⟅0, 1 # 2, 2 # 2, 3, 4, 5⟆ | ||
|
||
* The order of elements in a bag does not matter, but the number of | ||
copies of each element does matter. Two bags with the same elements | ||
in a different order are considered the same, whereas two bags with | ||
the same elements but a different number of copies of each are | ||
considered different. For example, | ||
|
||
:: | ||
|
||
Disco> ⟅1,2,3⟆ == ⟅1,3,2⟆ | ||
T | ||
Disco> ⟅1,2,3⟆ == ⟅1,1,2,3⟆ | ||
F | ||
|
||
* To check whether a bag contains a given element at least once, one can | ||
use the ``elem`` operator (also written ``∈``): | ||
|
||
:: | ||
|
||
Disco> 2 elem ⟅1,2,3⟆ | ||
T | ||
Disco> 5 elem ⟅1,2,3⟆ | ||
F | ||
Disco> 2 ∈ ⟅1,2,3⟆ | ||
T | ||
|
||
XXX operations on bags: intersection, union, power |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
Lists | ||
===== | ||
|
||
For any :doc:`type <types>` ``T``, ``List(T)`` is the type of *finite lists* with | ||
elements of type ``T``. A list is an arbitrary *sequence*; that is, a | ||
list is a collection of ``T`` values where the order matters. | ||
|
||
In general, lists are written with square brackets. | ||
|
||
* The empty list is written ``[]``. | ||
* A list with specific elements can be written like this: ``[1, 2, 3]``. | ||
* An :doc:`ellipsis <ellipsis>` can be used to generate a range of | ||
elements. For example, | ||
|
||
:: | ||
|
||
Disco> [1 .. 5] | ||
[1, 2, 3, 4, 5] | ||
Disco> [1, 3 .. 9] | ||
[1, 3, 5, 7, 9] | ||
|
||
* :doc:`List comprehension <comprehension>` notation can also be used, | ||
for example: | ||
|
||
:: | ||
|
||
Disco> [ abs(x - 5) | x in [1 .. 10], x^2 > 5] | ||
[2, 1, 0, 1, 2, 3, 4, 5] | ||
|
||
* The built-in ``list`` function can be used to convert other | ||
collections (*e.g.* :doc:`sets <set>` or :doc:`bags <bag>`) to lists: | ||
|
||
:: | ||
|
||
Disco> list({1, 2, 3}) | ||
[1, 2, 3] | ||
Disco> import num | ||
Disco> list(factor(12)) | ||
[2, 2, 3] | ||
|
||
* The built-in `append` function can be used to append two lists. | ||
|
||
:: | ||
|
||
Disco> import list | ||
Disco> :type append | ||
append : List(a) × List(a) → List(a) | ||
Disco> append([1,2,3], [5,6]) | ||
[1, 2, 3, 5, 6] | ||
|
||
* Strings (written in double quotes) are really just lists of characters. | ||
|
||
:: | ||
|
||
Disco> |"hello"| | ||
5 | ||
Disco> append("hello", "world") | ||
"helloworld" | ||
Disco> [(c,1) | c in "hello"] | ||
[('h', 1), ('e', 1), ('l', 1), ('l', 1), ('o', 1)] | ||
|
||
* The order of elements in a list matters. Two lists with elements in a | ||
different order, or different number of copies of the elements, are | ||
not the same. For example, | ||
|
||
:: | ||
|
||
Disco> [1,2,3] == [1,3,2] | ||
F | ||
Disco> [1,2,3] == [1,1,2,3] | ||
F | ||
|
||
* To check whether a list contains a given element, one can use the | ||
``elem`` operator (also written ``∈``): | ||
|
||
:: | ||
|
||
Disco> 2 elem [1,2,3] | ||
T | ||
Disco> 5 elem [1,2,3] | ||
F | ||
Disco> 2 ∈ [1,2,3] | ||
T | ||
|
||
Inductive definition of lists | ||
----------------------------- | ||
|
||
Write more here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters