Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #46202 > unrolled thread

Rosetta: Set

Started by"WJ" <w_a_x_man@yahoo.com>
First post2016-03-25 06:37 +0000
Last post2016-03-25 06:37 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.forth


Contents

  Rosetta: Set "WJ" <w_a_x_man@yahoo.com> - 2016-03-25 06:37 +0000

#46202 — Rosetta: Set

From"WJ" <w_a_x_man@yahoo.com>
Date2016-03-25 06:37 +0000
SubjectRosetta: Set
Message-ID<nd2mbo$d7q$1@dont-email.me>
Set creation
Test m ? S -- "m is an element in set S"
A ? B -- union; a set of all elements either in set A or in set B.
A n B -- intersection; a set of all elements in both set A and set B.
A \ B -- difference; a set of all elements in set A, except those in set B.
A ? B -- subset; true if every element in set A is also in set B.
A = B -- equality; true if every element of set A is in set B and vice-versa.


OCaml:

module SS = Set.Make(String);;

let s1 = SS.of_list
  ["foo"; "bar"; "of"; "in"; "on"; "over"; "anent"; "under"; "way"] ;;

let s2 = SS.of_list
  ["foo"; "on"; "under"; "way"; "sway"; "swept"; "link"];;

let s3 = SS.of_list
  ["bar"; "over"; "under"; "way"] ;;

SS.mem "anent" s1;;
 ===>
true

SS.elements (SS.union s1 s2);;
 ===>
["anent"; "bar"; "foo"; "in"; "link"; "of"; "on"; "over"; "sway"; "swept";
 "under"; "way"]

SS.elements (SS.inter s1 s2);;
 ===>
["foo"; "on"; "under"; "way"]

SS.elements (SS.diff s1 s2);;
 ===>
["anent"; "bar"; "in"; "of"; "over"]

SS.subset s3 s1;;
 ===>
true

SS.equal s3 s2;;
 ===>
false


How much easier would this be in ANS Forth?


-- 
Use this [sword] for me, if I rule well; if not, against me. --- Trajan
"If a government uses the instruments of power in its hands for the purpose of
leading a people to ruin, then rebellion is not only the right but also the
duty of every individual citizen."

[toc] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web