on 159efe7 help sign in
1208 lines 18 kB history raw
1+++
2title = "2b: List Logic"
3weight = 5
4template = "doc.html"
5+++
6### `++fand`
8All indices in `list`
10Produces the indices of all occurrences of `nedl` in `hstk` as a `list` of
11atoms.
13#### Accepts
15`nedl` is a list.
17`hstk` is a list.
19#### Produces
21A `list`.
23#### Source
25```
26 ++ fand
27 ~/ %fand
28 |= [nedl=(list) hstk=(list)]
29 =| i=@ud
30 =| fnd=(list @ud)
31 |- ^+ fnd
32 =+ [n=nedl h=hstk]
33 |-
34 ?: |(?=($~ n) ?=($~ h))
35 (flop fnd)
36 ?: =(i.n i.h)
37 ?~ t.n
38 ^$(i +(i), hstk +.hstk, fnd [i fnd])
39 $(n t.n, h t.h)
40 ^$(i +(i), hstk +.hstk)
41```
43#### Examples
45```
46 > (fand ~[3] ~[1 2 3])
47 ~[2]
49 > (fand ~[4] ~[1 2 3])
50 ~
52 > (fand ~['a'] "cbabab")
53 ~[2 4]
55 > (fand "ba" "cbabab")
56 ~[1 3]
57```
60---
61### `++find`
63First index in `list`
65Produces the index of the first occurrence of `nedl` in `hstk` as the `unit`
66of an atom.
68#### Accepts
70`nedl` is a list.
72`hstk` is a list.
74#### Produces
76The `unit` of an atom.
78#### Source
80```
81 ++ find
82 ~/ %find
83 |= [nedl=(list) hstk=(list)]
84 =| i=@ud
85 |- ^- (unit @ud)
86 =+ [n=nedl h=hstk]
87 |-
88 ?: |(?=($~ n) ?=($~ h))
89 ~
90 ?: =(i.n i.h)
91 ?~ t.n
92 `i
93 $(n t.n, h t.h)
94 ^$(i +(i), hstk +.hstk)
95```
97#### Examples
99```
100 > (find [3]~ ~[1 2 3])
101 [~ u=2]
103 > (find [4]~ ~[1 2 3])
104 ~
106 > (find ['c']~ "cbabab")
107 [~ u=0]
109 > (find "ab" "cbabab")
110 [~ u=1]
112 > (find "bab" "cbabab")
113 [~ u=2]
114```
116---
117### `++flop`
119Reverse
121Produces the `list` `a` in reverse order.
123#### Accepts
125`a` is a `list`.
127#### Produces
129A `list`.
131#### Source
133```
134 ++ flop
135 ~/ %flop
136 |* a=(list)
137 => .(a (homo a))
138 ^+ a
139 =+ b=`_a`~
140 |-
141 ?~ a b
142 $(a t.a, b [i.a b])
143```
146#### Examples
148```
149 > =a [1 2 3 ~]
150 > (flop a)
151 ~[3 2 1]
153 > (flop (flop a))
154 ~[1 2 3]
155```
157---
158### `++gulf`
160List from range
162Produces a `list` composed of each consecutive integer starting from `a` and
163ending with `b`. `a` and `b` are themselves included.
165#### Accepts
167`a` is an atom.
169`b` is an atom.
171#### Produces
173a `list`.
175#### Source
177```
178 ++ gulf
179 |= [a=@ b=@]
180 ^- (list @)
181 ?:(=(a +(b)) ~ [a $(a +(a))])
182```
184#### Examples
186```
187 > (gulf 1 6)
188 ~[1 2 3 4 5 6]
190 > `(list @t)`(gulf 99 106)
191 <|c d e f g h i j|>
192```
194---
195### `++homo`
197Homogenize
199Produces a `list` whose type is a fork of all the contained types in the
200list `a`. Used when you want to make all the types of the elements of a list the
201same.
203#### Accepts
205`a` is a `list`.
207#### Produces
209a `list`.
211#### Source
213```
214 ++ homo
215 |* a=(list)
216 ^+ =< $
217 |% +- $ ?:(*? ~ [i=(snag 0 a) t=$])
218 --
219 a
220```
222#### Examples
224```
225 > lyst
226 [i=1 t=[i=97 t=[i=2 t=[i=98 t=[i=[~ u=10] t=~]]]]]
228 > (homo lyst)
229 ~[1 97 2 98 [~ u=10]]
231 > =a (limo [1 2 3 ~])
232 > a
233 [i=1 t=[i=2 t=[i=3 t=~]]]
235 > (homo a)
236 ~[1 2 3]
237```
240---
241### `++lent`
243List length
245Produces the length of any `list` `a` as an atom.
247#### Accepts
249`a` is a `list`.
251#### Produces
253an atom.
255#### Source
257```
258 ++ lent
259 ~/ %lent
260 |= a=(list)
261 ^- @
262 =+ b=0
263 |-
264 ?~ a b
265 $(a t.a, b +(b))
266```
268#### Examples
270```
271 > (lent [1 2 3 4 ~]))
272 4
274 > (lent [1 'a' 2 'b' (some 10) ~])
275 5
276```
279---
280### `++levy`
282Logical "and" on list
284Computes the Boolean logical "and" on the results of gate `b` applied to each individual element in `list` `a`.
286#### Accepts
288`a` is a list.
290`b` is a gate.
292#### Produces
294A boolean.
296#### Source
298```
299 ++ levy
300 ~/ %levy :: all of
301 |* [a=(list) b=$-(* ?)]
302 |- ^- ?
303 ?~ a &
304 ?. (b i.a) |
305 $(a t.a)
306```
308#### Examples
310```
311 > =a |=(a=@ (lte a 1))
312 > (levy `(list @)`[0 1 2 1 ~] a)
313 %.n
315 > =a |=(a=@ (lte a 3))
316 > (levy `(list @)`[0 1 2 1 ~] a)
317 %.y
318```
321---
323### `++lien`
325Logical "or" on list
327Computes the Boolean logical "or" on the results of applying `gate` `b` to
328every element of `++list` `a`.
330#### Accepts
332`a` is a list.
334`b` is a gate.
336#### Source
338```
339 ++ lien
340 ~/ %lien
341 |* [a=(list) b=$-(* ?)]
342 |- ^- ?
343 ?~ a |
344 ?: (b i.a) &
345 $(a t.a)
346```
348#### Examples
350```
351 > =a |=(a=@ (gte a 1))
352 > (lien `(list @)`[0 1 2 1 ~] a)
353 %.y
355 > =a |=(a=@ (gte a 3))
356 > (lien `(list @)`[0 1 2 1 ~]) a)
357 %.n
358```
361---
362### `++limo`
364List Constructor
366Turns a null-terminated tuple into a `list`.
368#### Accepts
370`a` is a null-terminated tuple.
372#### Produces
374A `++list`.
376#### Source
378```
379 ++ limo :: listify
380 |* a=*
381 ^+ =< $
382 |% +- $ ?~(a ~ ?:(_? i=-.a t=$ $(a +.a)))
383 --
384 a
385```
387#### Examples
389```
390 > (limo [1 2 3 ~])
391 [i=1 t=[i=2 t=[i=3 t=~]]]
392```
395---
396### `++murn`
398Maybe transform
400Passes each member of `list` `a` to gate `b`, which must produce a
401`unit`. Produces a new list with all the results that do not produce
402`~`.
404#### Accepts
406`a` is a list.
408`b` is a gate that produces a unit.
410#### Produces
412A unit.
414#### Source
416```
417 ++ murn :: maybe transform
418 ~/ %murn
419 |* [a=(list) b=$-(* (unit))]
420 |-
421 ?~ a ~
422 =+ c=(b i.a)
423 ?~ c
424 $(a t.a)
425 [i=u.c t=$(a t.a)]
426```
428#### Examples
430```
431 > =a |=(a=@ ?.((gte a 2) ~ (some (add a 10))))
432 > (murn `(list @)`[0 1 2 3 ~] a)
433 [i=12 t=[i=13 t=~]]
434```
437---
438### `++oust`
440Remove
442Removes elements from list `c` beginning at inclusive index `a`, removing `b`
443number of elements.
445#### Accepts
447`c` is a list.
449#### Produces
451A `++list`.
453#### Source
455```
456 ++ oust :: remove
457 ~/ %oust
458 |* [[a=@ b=@] c=(list)]
459 (weld (scag a c) (slag (add a b) c))
460```
462#### Examples
464```
465 > (oust [4 5] "good day, urbit!")
466 "good urbit!"
468 > (oust [2 2] `(list @)`[1 2 3 4 ~])
469 ~[1 2]
470```
473---
474### `++reap`
476Replicate
478Replicate: produces a `list` containing `a` copies of `b`.
480#### Accepts
482`a` is an atom.
484`b` is a noun.
486#### Produces
488A list.
490#### Source
492```
493 ++ reap :: replicate
494 ~/ %reap
495 |* [a=@ b=*]
496 |- ^- (list _b)
497 ?~ a ~
498 [b $(a (dec a))]
499```
501#### Examples
503```
504 > (reap 20 %a)
505 ~[%a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a]
507 > (reap 5 ~s1)
508 ~[~s1 ~s1 ~s1 ~s1 ~s1]
510 > `@dr`(roll (reap 5 ~s1) add)
511 ~s5
512```
515---
516### `++reel`
518Right fold
520Right fold: moves right to left across a `list` `a`, recursively slamming
521a binary gate `b` with an element from `a` and an accumulator, producing
522the final value of the accumulator.
524(To "slam" means to call a gate and give it a sample/samples. In this instance,
525`a` is the list of samples that are given to the gate b.)
527#### Accepts
529`a` is a list.
531`b` is a binary gate.
533#### Produces
535The accumulator, which is a noun.
537#### Source
539```
540 ++ reel
541 ~/ %reel
542 |* [a=(list) b=_|=([* *] +<+)]
543 |- ^+ +<+.b
544 ?~ a
545 +<+.b
546 (b i.a $(a t.a))
547```
549#### Examples
551```
552 > =sum =|([p=@ q=@] |.((add p q)))
553 > (reel (limo [1 2 3 4 5 ~]) sum)
554 15
556 > =a =|([p=@ q=@] |.((sub p q)))
557 > (reel (limo [6 3 1 ~]) a)
558 4
560 > (reel (limo [3 6 1 ~]) a)
561 ! subtract-underflow
562 ! exit
563```
566---
567### `++roll`
569Left fold
571Left fold: moves left to right across a list `a`, recursively slamming a
572binary gate `b` with an element from the `list` and an accumulator,
573producing the final value of the accumulator.
575(To "slam" means to call a gate and give it a sample/samples. In this instance,
576`a` is the list of samples that are given to the gate b.)
578#### Accepts
580`a` is a list.
582`b` is a binary gate.
584#### Produces
586The accumulator, which is a noun.
588#### Source
590```
591 ++ roll :: left fold
592 ~/ %roll
593 |* [a=(list) b=_|=([* *] +<+)]
594 |- ^+ +<+.b
595 ?~ a
596 +<+.b
597 $(a t.a, b b(+<+ (b i.a +<+.b)))
598```
600#### Examples
602```
603 > =sum =|([p=@ q=@] |.((add p q)))
604 > (roll (limo [1 2 3 4 5 ~]) sum)
605 q=15
607 > =a =|([p=@ q=@] |.((sub p q)))
608 > (roll (limo [6 3 1 ~]) a)
609 ! subtract-underflow
610 ! exit
612 > (roll (limo [1 3 6 ~]) a)
613 q=4
614```
617---
618### `++scag`
620Prefix
622Accepts an atom `a` and `list` `b`, producing the first `a` elements of
623the front of the list.
625#### Accepts
627`a` is an atom.
629`b` is a list.
631#### Produces
633A list of the same type as `b`.
635#### Source
637```
638 ++ scag :: prefix
639 ~/ %scag
640 |* [a=@ b=(list)]
641 |- ^+ b
642 ?: |(?=(~ b) =(0 a)) ~
643 [i.b $(b t.b, a (dec a))]
644```
646#### Examples
648```
649 > (scag 2 `(list @)`[1 2 3 4 ~])
650 [i=0 t=~[1]]
652 > (scag 10 `(list @)`[1 2 3 4 ~])
653 [i=1 t=~[2 3 4]]
654```
657---
658### `++skid`
660Separate
662Separates a `list` `a` into two lists - Those elements of `a` who produce
663true when slammed to gate `b` and those who produce `%.n`.
665(To "slam" means to call a gate and give it a sample/samples. In this instance,
666`a` is the list of samples that are given to the gate `b`.)
668#### Accepts
670`a` is a list.
672`b` is a gate that accepts one argument and produces a flag.
674#### Produces
676A cell of two lists.
678#### Source
680```
681 ++ skid :: separate
682 ~/ %skid
683 |* [a=(list) b=$-(* ?)]
684 |- ^+ [p=a q=a]
685 ?~ a [~ ~]
686 =+ c=$(a t.a)
687 ?:((b i.a) [[i.a p.c] q.c] [p.c [i.a q.c]])
688```
691#### Examples
693```
694 > =a |=(a=@ (gth a 1))
695 > (skid `(list @)`[0 1 2 3 ~]) a)
696 (skid `(list @)`[0 1 2 3 ~]) a)
697```
700---
701### `++skim`
703Suffix
705Cycles through the members of a list `a`, passing them to a gate `b` and
706producing a list of all of the members that produce `%.y`. Inverse of
707`skip`.
709#### Accepts
711`a` is a list.
713`b` is a gate that accepts one argument and produces a boolean.
715#### Produces
717A flag.
719#### Source
721```
722 ++ skim :: only
723 ~/ %skim
724 |* [a=(list) b=$-(* ?)]
725 |-
726 ^+ a
727 ?~ a ~
728 ?:((b i.a) [i.a $(a t.a)] $(a t.a))
729```
731#### Examples
733```
734 > =a |=(a=@ (gth a 1))
735 > (skim `(list @)`[0 1 2 3 ~] a)
736 [i=2 t=~[3]]
737```
739---
740### `++skip`
742Except
744Cycles through the members of `list` `a`, passing them to a gate `b`.
745Produces a list of all of the members that produce `%.n`. Inverse of
746`skim`.
748#### Accepts
750`a` is a list.
752`b` is a gate that accepts one argument and produces a flag.
754#### Produces
756A list of the same type as `a`.
758#### Source
760```
761 ++ skip :: except
762 ~/ %skip
763 |* [a=(list) b=$-(* ?)]
764 |-
765 ^+ a
766 ?~ a ~
767 ?:((b i.a) $(a t.a) [i.a $(a t.a)])
768```
770#### Examples
772```
773 > =a |=(a=@ (gth a 1))
774 > (skip `(l)`[0 1 2 3 ~]) a)
775 [i=0 t=[i=1 t=~]]
776```
779---
780### `++slag`
782Suffix
784Accepts an atom `a` and list `b`, producing the remaining elements from
785`b` starting at `a`.
787#### Accepts
789`b` is a list.
791#### Produces
793A list of the same type as `b`.
795#### Source
797```
798 ++ slag :: suffix
799 ~/ %slag
800 |* [a=@ b=(list)]
801 |- ^+ b
802 ?: =(0 a) b
803 ?~ b ~
804 $(b t.b, a (dec a))
805```
807#### Examples
809```
810 > (slag 2 (limo [1 2 3 4 ~]))
811 [i=3 t=[i=4 t=~]]
812 > (slag 1 (limo [1 2 3 4 ~]))
813 [i=2 t=[i=3 t=[i=4 t=~]]]
814```
817---
818### `++snag`
820Index
822Accepts an atom `a` and a `++list` `b`, producing the element at the index
823of `a`and failing if the list is null. Lists are 0-indexed.
825#### Accepts
827`b` is a list.
829#### Produces
831Produces an element of `b`, or crashes if no element exists at that index.
833#### Source
835```
836 ++ snag :: index
837 ~/ %snag
838 |* [a=@ b=(list)]
839 |-
840 ?~ b
841 ~|('snag-fail' !!)
842 ?: =(0 a) i.b
843 $(b t.b, a (dec a))
844```
846#### Examples
848```
849 > (snag 2 "asdf")
850 ~~d
851 > (snag 0 `(list @ud)`~[1 2 3 4])
852 1
853```
856---
857### `++sort`
859Quicksort
861Quicksort: accepts a `++list` `a` and a gate `b` which accepts two nouns and
862produces a flag. `++sort` then produces a list of the elements of `a`,
863sorted according to `b`.
865#### Accepts
867`b` is a gate that accepts two nouns and produces a boolean.
869#### Produces
871A list
873#### Source
875```
876 ++ sort !. :: quicksort
877 ~/ %sort
878 |* [a=(list) b=$-([* *] ?)]
879 => .(a ^.(homo a))
880 |- ^+ a
881 ?~ a ~
882 %+ weld
883 $(a (skim t.a |=(c/_i.a (b c i.a))))
884 ^+ t.a
885 [i.a $(a (skim t.a |=(c/_i.a !(b c i.a))))]
886```
888#### Examples
890```
891 > =a =|([p/@ q/@] |.((gth p q)))
892 > (sort (limo [0 1 2 3 ~]) a)
893 ~[3 2 1 0]
894```
897---
898### `++spin`
900Gate to list, with state
902Accepts a `++list` `a`, a gate `b`, and some state `c`. Produces a list with
903the gate applied to each element of the original list. `b` is called with
904a tuple -- the head is an element of `a` and the tail is the state `c`, and
905should produce a tuple of the transformed element and the (potentially
906modified) state `c`.
908#### Accepts
910`a` is a `++list`.
912`b` is a gate.
914`c` is a noun.
916#### Produces
918A list.
920#### Source
922```
923 ++ spin
924 ~/ %spin
925 |* [a=(list) b=* c=_|=(^ [** +<+])]
926 => .(c `$-([_?>(?=(^ a) i.a) _b] [_-:(c) _b])`c)
927 =/ acc=(list _-:(c)) ~
928 |- ^- (pair _acc _b)
929 ?~ a
930 [(flop acc) b]
931 =^ res b (c i.a b)
932 $(acc [res acc], a t.a)
933```
935#### Examples
937```
938 > %^ spin (limo ~[4 5 6]) :: trivial example -- does nothing with the state
939 |=([n=@ a=@] [n a])
940 0
941 ~[4 5 6]
943 > %^ spin (limo ~[4 5 6]) :: `p` as the index and `q` as the list element
944 |=([n=@ a=@] [`(pair)`[a n] +(a)])
945 0
946 ~[[p=0 q=4] [p=1 q=5] [p=2 q=6]]
948 > %^ spin (reap 10 0) :: Create 10 random numbers less than `10`
949 |=([n=@ rng=_og] (rads:rng 10))
950 ~(. og eny)
951 ~[5 5 9 4 1 7 9 9 9 6]
952```
954#### Discussion
956`(~(rads og eny) 2)` creates a random number less than `2`, seeding the RNG
957with entropy (`eny`). The head of the product is the random number, the tail
958is the continuation of the RNG.
962---
963### `++spun`
965Gate to list, with state
967Accepts a `list` `a` and a gate `b`. `c` is internal state, initially
968derived by **bunting** the tail of the sample of gate `b`, instead of
969being passed in explicitly as in `++spin`. Produces a list with the
970gate applied to each element of the original list. `b` is called with a tuple --
971the head is an element of `a` and the tail is the state `c`, and should
972produce a tuple of the transformed element and the (potentially modified)
973state `c`.
975#### Accepts
977`a` is a `++list`.
979`b` is a gate.
981#### Produces
983A list.
985#### Source
987```
988 ++ spun
989 ~/ %spun
990 |* [a=(list) b=_|=(^ [** +<+])]
991 p:(spin a +<+.b b)
992```
994#### Examples
996```
997 > %+ spun (limo ~[4 5 6]) :: `p` as the index and `q` as the list element
998 |=([n=@ a=@] [`(pair)`[a n] +(a)])
999 ~[[p=0 q=4] [p=1 q=5] [p=2 q=6]]
1001 > =l (limo ~[7 8 9])
1002 > %+ spun (limo ~[4 5 6]) :: joins two lists into a list of pairs
1003 |=([n=@ a=@] [`(pair)`[(snag a l) n] +(a)])
1004 ~[[p=7 q=4] [p=8 q=5] [p=9 q=6]]
1005```
1008---
1009### `++swag`
1011Infix
1013Similar to `substr` in Javascript: extracts a string infix, beginning at
1014inclusive index `a`, producing `b` number of characters.
1016#### Accepts
1018`c` is a list.
1020#### Produces
1022A list of the same type as `c`.
1024#### Source
1026```
1027 ++ swag
1028 |* [[a=@ a=@] c=(list)]
1029 (scag +<-> (slag +<-< c))
1030```
1032#### Examples
1034```
1035 > (swag [2 5] "roly poly")
1036 "ly po"
1038 > (swag [2 2] (limo [1 2 3 4 ~]))
1039 [i=3 t=[i=4 t=~]]
1040```
1043---
1044### `++turn`
1046Gate to list
1048Accepts a `++list` `a` and a gate `b`. Produces a list with the gate applied
1049to each element of the original list.
1051#### Accepts
1053`b` is a gate.
1055#### Produces
1057A list.
1059#### Source
1061```
1062 ++ turn
1063 ~/ %turn
1064 |* [a=(list) b=gate]
1065 |-
1066 ?~ a ~
1067 [i=(b i.a) t=$(a t.a)]
1068```
1070#### Examples
1072```
1073 > (turn (limo [104 111 111 110 ~]) @t)
1074 <|h o o n|>
1076 > =a |=(a=@ (add a 4))
1077 > (turn (limo [1 2 3 4 ~]) a)
1078 ~[5 6 7 8]
1079```
1081#### Discussion
1083`turn` is Hoon's version of 'map' in Haskell.
1085---
1086### `++weld`
1088Concatenate
1090Concatenate two `++list`s `a` and `b`.
1092#### Accepts
1094`a` and `b` are lists.
1096#### Source
1098```
1099 ++ weld
1100 ~/ %weld
1101 |* [a=(list) b=(list)]
1102 => .(a ^.(homo a), b ^.(homo b))
1103 |- ^+ b
1104 ?~ a b
1105 [i.a $(a t.a)]
1106```
1108#### Examples
1110```
1111 > (weld "urb" "it")
1112 ~[~~u ~~r ~~b ~~i ~~t]
1114 > (weld (limo [1 2 ~]) (limo [3 4 ~]))
1115 ~[1 2 3 4]
1116```
1119---
1120### `++welp`
1122Perfect weld
1124Concatenate two `++list`s `a` and `b` without losing their type information
1125to homogenization.
1127#### Accepts
1129`a` is a list.
1131`b` is a list.
1133#### Produces
1135A list.
1137#### Source
1139```
1140 ++ welp
1141 =| [* *]
1142 |%
1143 +- $
1144 ?~ +<-
1145 +<-(. +<+)
1146 +<-(+ $(+<- +<->))
1147 --
1148```
1150#### Examples
1152```
1153 > (welp "foo" "bar")
1154 "foobar"
1156 > (welp ~[60 61 62] ~[%a %b %c])
1157 [60 61 62 %a %b %c ~]
1159 > ? (welp ~[60 61 62] ~[%a %b %c])
1160 {@ud @ud @ud $a $b $c $~}
1161 [60 61 62 %a %b %c ~]
1163 > (welp [sa+1 so+2 ~] si=3)
1164 [[%sa 1] [%so 2] si=3]
1165```
1168---
1169### `++zing`
1171Cons
1173Turns a `++list` of lists into a single list by promoting the elements of
1174each sublist into the higher.
1176#### Accepts
1178A list of lists.
1180#### Produces
1182A list.
1184#### Source
1186```
1187 ++ zing
1188 =| *
1189 |%
1190 +- $
1191 ?~ +<
1192 +<
1193 (welp +<- $(+< +<+))
1194 --
1195```
1198#### Examples
1200```
1201 > (zing (limo [(limo ['a' 'b' 'c' ~]) (limo ['e' 'f' 'g' ~]) (limo ['h' 'i' 'j' ~]) ~]))
1202 ~['a' 'b' 'c' 'e' 'f' 'g' 'h' 'i' 'j']
1204 > (zing (limo [(limo [1 'a' 2 'b' ~]) (limo [3 'c' 4 'd' ~]) ~]))
1205 ~[1 97 2 98 3 99 4 100]
1206```
1208---