on 4fdc19d help sign in
1645 lines 22 kB history raw
1+++
2title = "2b: List Logic"
3weight = 5
4template = "doc.html"
5+++
7## `++bake`
9**Note:** This function isn't specifically a list function but is included in section 2b of the standard library so is documented here for completeness.
11Convert wet gate `f` to a dry gate by specifying argument mold `a`.
13`+bake` is a wet gate that takes a wet gate and produces a dry gate.
15#### Accepts
17`f` is a `gate`.
19`a` is a `mold`.
21#### Produces
23A dry `gate` whose sample type is `a`.
25#### Source
27```hoon
28++ bake
29 |* [f=gate a=mold]
30 |= arg=a
31 (f arg)
32```
34#### Examples
36```
37> =wet-gate |*(a=* [a a])
38> (wet-gate 42)
39[42 42]
40> (wet-gate ['foo' 'bar'])
41[['foo' 'bar'] 'foo' 'bar']
42> =dry-gate (bake wet-gate @ud)
43> (dry-gate 42)
44[42 42]
45> (dry-gate ['foo' 'bar'])
46-need.@ud
47-have.[@t @t]
48nest-fail
49```
51---
53## `++fand`
55All indices in `list`
57Produces the indices of all occurrences of `nedl` in `hstk` as a `list` of
58atoms.
60#### Accepts
62`nedl` is a list.
64`hstk` is a list.
66#### Produces
68A `list`.
70#### Source
72```hoon
73++ fand
74 ~/ %fand
75 |= [nedl=(list) hstk=(list)]
76 =| i=@ud
77 =| fnd=(list @ud)
78 |- ^+ fnd
79 =+ [n=nedl h=hstk]
80 |-
81 ?: |(?=(~ n) ?=(~ h))
82 (flop fnd)
83 ?: =(i.n i.h)
84 ?~ t.n
85 ^$(i +(i), hstk +.hstk, fnd [i fnd])
86 $(n t.n, h t.h)
87 ^$(i +(i), hstk +.hstk)
88```
90#### Examples
92```
93> (fand ~[3] ~[1 2 3])
94~[2]
95```
97```
98> (fand ~[4] ~[1 2 3])
100```
102```
103> (fand ~['a'] "cbabab")
104~[2 4]
105```
107```
108> (fand "ba" "cbabab")
109~[1 3]
110```
112---
114## `++find`
116First index in `list`
118Produces the index of the first occurrence of `nedl` in `hstk` as the `unit`
119of an atom.
121#### Accepts
123`nedl` is a list.
125`hstk` is a list.
127#### Produces
129The `unit` of an atom.
131#### Source
133```hoon
134++ find
135 ~/ %find
136 |= [nedl=(list) hstk=(list)]
137 =| i=@ud
138 |- ^- (unit @ud)
139 =+ [n=nedl h=hstk]
140 |-
141 ?: |(?=(~ n) ?=(~ h))
142 ~
143 ?: =(i.n i.h)
144 ?~ t.n
145 `i
146 $(n t.n, h t.h)
147 ^$(i +(i), hstk +.hstk)
148```
150#### Examples
152```
153> (find [3]~ ~[1 2 3])
154[~ u=2]
155```
157```
158> (find [4]~ ~[1 2 3])
160```
162```
163> (find ['c']~ "cbabab")
164[~ u=0]
165```
167```
168> (find "ab" "cbabab")
169[~ u=2]
170```
172```
173> (find "bab" "cbabab")
174[~ u=1]
175```
177---
179## `++flop`
181Reverse
183Produces the `list` `a` in reverse order.
185#### Accepts
187`a` is a `list`.
189#### Produces
191A `list`.
193#### Source
195```hoon
196++ flop
197 ~/ %flop
198 |* a=(list)
199 => .(a (homo a))
200 ^+ a
201 =+ b=`_a`~
202 |-
203 ?~ a b
204 $(a t.a, b [i.a b])
205```
207#### Examples
209```
210> =a [1 2 3 ~]
211> (flop a)
212~[3 2 1]
213```
215```
216> (flop (flop a))
217~[1 2 3]
218```
220---
222## `++gulf`
224List from range
226Produces a `list` composed of each consecutive integer starting from `a` and
227ending with `b`. `a` and `b` are themselves included.
229#### Accepts
231`a` is an atom.
233`b` is an atom.
235#### Produces
237a `list`.
239#### Source
241```hoon
242++ gulf
243 |= [a=@ b=@]
244 ?> (lte a b)
245 |- ^- (list @)
246 ?:(=(a +(b)) ~ [a $(a +(a))])
247```
249#### Examples
251```
252> (gulf 1 6)
253~[1 2 3 4 5 6]
254```
256```
257> `(list @t)`(gulf 99 106)
258<|c d e f g h i j|>
259```
261---
263## `++homo`
265Homogenize
267Produces a `list` whose type is a fork of all the contained types in the
268list `a`. Used when you want to make all the types of the elements of a list the
269same.
271#### Accepts
273`a` is a `list`.
275#### Produces
277a `list`.
279#### Source
281```hoon
282++ homo
283 |* a=(list)
284 ^+ =< $
285 |@ ++ $ ?:(*? ~ [i=(snag 0 a) t=$])
286 --
287 a
288```
290#### Examples
292```
293> lyst
294[i=1 t=[i=97 t=[i=2 t=[i=98 t=[i=[~ u=10] t=~]]]]]
295> (homo lyst)
296~[1 97 2 98 [~ u=10]]
297```
299```
300> =a (limo [1 2 3 ~])
301> a
302[i=1 t=[i=2 t=[i=3 t=~]]]
303> (homo a)
304~[1 2 3]
305```
307---
309## `++into`
311Insert item at index
313Accepts a `list` `a`, an atom `b`, and a noun `c`, producing the list of `a` with the item `c` inserted at index `b`.
315#### Accepts
317`a` is a list.
319`b` is a atom.
321`c` is a noun.
323#### Produces
325the list of `a` with the item `c` inserted at index `b`.
327#### Source
329```hoon
330++ into
331 ~/ %into
332 |* [a=(list) b=@ c=*]
333 ^+ a
334 (weld (scag b a) [c (slag b a)])
335```
337#### Examples
339```
340> (into (limo ~[2 3 4]) 1 11)
341~[2 11 3 4]
342```
344---
346## `++join`
348Constructs a new `list`, placing `sep` between every element of `lit`.
350#### Accepts
352`sep` is a `noun`.
354`lit` is a `list`.
356#### Produces
358a `list`.
360#### Source
362```hoon
363++ join
364 |* [sep=* lit=(list)]
365 =. sep `_?>(?=(^ lit) i.lit)`sep
366 ?~ lit ~
367 =| out=(list _?>(?=(^ lit) i.lit))
368 |- ^+ out
369 ?~ t.lit
370 (flop [i.lit out])
371 $(out [sep i.lit out], lit t.lit)
372```
374#### Examples
376```
377> (join ' ' "hoon")
378"h o o n"
379```
381```
382> (join 0 `(list @)`~[1 2 3])
383~[1 0 2 0 3]
384```
386---
388## `++lent`
390List length
392Produces the length of any `list` `a` as an atom.
394#### Accepts
396`a` is a `list`.
398#### Produces
400an atom.
402#### Source
404```hoon
405++ lent
406 ~/ %lent
407 |= a=(list)
408 ^- @
409 =+ b=0
410 |-
411 ?~ a b
412 $(a t.a, b +(b))
413```
415#### Examples
417```
418> (lent [1 2 3 4 ~]))
420```
422```
423> (lent [1 'a' 2 'b' (some 10) ~])
425```
427---
429## `++levy`
431Logical "and" on list
433Computes the Boolean logical "and" on the results of gate `b` applied to each individual element in `list` `a`.
435#### Accepts
437`a` is a list.
439`b` is a gate.
441#### Produces
443A boolean.
445#### Source
447```hoon
448++ levy
449 ~/ %levy
450 |* [a=(list) b=$-(* ?)]
451 |- ^- ?
452 ?~ a &
453 ?. (b i.a) |
454 $(a t.a)
455```
457#### Examples
459```
460> =a |=(a=@ (lte a 1))
461> (levy `(list @)`[0 1 2 1 ~] a)
462%.n
463```
465```
466> =a |=(a=@ (lte a 3))
467> (levy `(list @)`[0 1 2 1 ~] a)
468%.y
469```
471---
473## `++lien`
475Logical "or" on list
477Computes the Boolean logical "or" on the results of applying `gate` `b` to
478every element of `++list` `a`.
480#### Accepts
482`a` is a list.
484`b` is a gate.
486#### Source
488```hoon
489++ lien
490 ~/ %lien
491 |* [a=(list) b=$-(* ?)]
492 |- ^- ?
493 ?~ a |
494 ?: (b i.a) &
495 $(a t.a)
496```
498#### Examples
500```
501> =a |=(a=@ (gte a 1))
502> (lien `(list @)`[0 1 2 1 ~] a)
503%.y
504```
506```
507> =a |=(a=@ (gte a 3))
508> (lien `(list @)`[0 1 2 1 ~]) a)
509%.n
510```
512---
514## `++limo`
516List Constructor
518Turns a null-terminated tuple into a `list`.
520#### Accepts
522`a` is a null-terminated tuple.
524#### Produces
526A `++list`.
528#### Source
530```hoon
531++ limo
532 |* a=*
533 ^+ =< $
534 |@ ++ $ ?~(a ~ ?:(*? [i=-.a t=$] $(a +.a)))
535 --
536 a
537```
539#### Examples
541```
542> (limo [1 2 3 ~])
543[i=1 t=[i=2 t=[i=3 t=~]]]
544```
546---
548## `++murn`
550Maybe transform
552Passes each member of `list` `a` to gate `b`, which must produce a
553`unit`. Produces a new list with all the results that do not produce
554`~`.
556#### Accepts
558`a` is a list.
560`b` is a gate that produces a unit.
562#### Produces
564A list.
566#### Source
568```hoon
569++ murn
570 ~/ %murn
571 |* [a=(list) b=$-(* (unit))]
572 => .(a (homo a))
573 |- ^- (list _?>(?=(^ a) (need (b i.a))))
574 ?~ a ~
575 =/ c (b i.a)
576 ?~ c $(a t.a)
577 [+.c $(a t.a)]
578```
580#### Examples
582```
583> =a |=(a=@ ?.((gte a 2) ~ (some (add a 10))))
584> (murn `(list @)`[0 1 2 3 ~] a)
585[i=12 t=[i=13 t=~]]
586```
588---
590## `++oust`
592Remove
594Removes elements from list `c` beginning at inclusive index `a`, removing `b`
595number of elements.
597#### Accepts
599`c` is a list.
601#### Produces
603A `++list`.
605#### Source
607```hoon
608++ oust
609 ~/ %oust
610 |* [[a=@ b=@] c=(list)]
611 (weld (scag +<-< c) (slag (add +<-< +<->) c))
612```
614#### Examples
616```
617> (oust [4 5] "good day, urbit!")
618"good urbit!"
619```
621```
622> (oust [2 2] `(list @)`[1 2 3 4 ~])
623~[1 2]
624```
626---
628## `++reap`
630Replicate
632Replicate: produces a `list` containing `a` copies of `b`.
634#### Accepts
636`a` is an atom.
638`b` is a noun.
640#### Produces
642A list.
644#### Source
646```hoon
647++ reap
648 ~/ %reap
649 |* [a=@ b=*]
650 |- ^- (list _b)
651 ?~ a ~
652 [b $(a (dec a))]
653```
655#### Examples
657```
658> (reap 20 %a)
659~[%a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a]
660```
662```
663> (reap 5 ~s1)
664~[~s1 ~s1 ~s1 ~s1 ~s1]
665> `@dr`(roll (reap 5 ~s1) add)
666~s5
667```
669---
671## `++rear`
673Last item of list
675Produces the last item in list `a`, crashing if `a` is null.
677#### Accepts
679`a` is a `list`.
681#### Produces
683The type of the last element in `a`.
685#### Source
687```hoon
688++ rear
689 ~/ %rear
690 |* a=(list)
691 ^- _?>(?=(^ a) i.a)
692 ?> ?=(^ a)
693 ?: =(~ t.a) i.a
694 $(a t.a)
695```
697#### Examples
699```
700> (rear ~[1 2 3])
702```
704```
705> (rear ~)
706dojo: hoon expression failed
707```
709---
711## `++reel`
713Right fold
715Right fold: moves right to left across a `list` `a`, recursively slamming
716a binary gate `b` with an element from `a` and an accumulator, producing
717the final value of the accumulator.
719(To "slam" means to call a gate and give it a sample/samples. In this instance,
720`a` is the list of samples that are given to the gate b.)
722The initial value of the accumulator is the bunt of `b`'s second argument (`+<+`). This can occasionally produce
723undesired behavior (see examples). If you need more control over the initial
724value, try making use of `$_` and `|:`, or perhaps [`+spin`](#spin) or
725[`+spun`](#spun).
727#### Accepts
729`a` is a list.
731`b` is a binary gate.
733#### Produces
735The accumulator, which is a noun.
737#### Source
739```hoon
740++ reel
741 ~/ %reel
742 |* [a=(list) b=_=>(~ |=([* *] +<+))]
743 |- ^+ ,.+<+.b
744 ?~ a
745 +<+.b
746 (b i.a $(a t.a))
747```
749#### Examples
751```
752> (reel `(list @)`[1 2 3 4 5 ~] add)
75315
755> (reel `(list @)`[6 3 1 ~] sub)
758> (reel `(list @)`[3 6 1 ~] sub)
759! subtract-underflow
760! exit
761```
763`+mul`'s default sample is 1, so calling `+reel` with `+mul` yields the expected behavior:
765```
766> *mul
769> (reel `(list @)`~[1 2 3 4] mul)
77024
771```
773However, if you build a gate that uses `+mul` like so, the sample defaults to 0
774since that is the bunt of `@`:
776```
777> (reel `(list @)`~[1 2 3 4] |=([a=@ b=@] (mul a b)))
779```
781We can fix this with `|:`:
783```
784> (reel `(list @)`~[1 2 3 4] |:([a=1 b=1] (mul a b)))
78524
786```
788If you check the definition of `+mul`, you'll see that it also utilizes this
789pattern.
791We can check explicitly what sequence of operations `+reel` performs like this:
792```
793> =f |: [l='e_l' r='e_r']
794 ^- @t
795 :((cury cat 3) '(' l '*' r ')')
796> (reel "abcde" f)
797'(a*(b*(c*(d*(e*e_r)))))'
798```
801---
803## `++roll`
805Left fold
807Left fold: moves left to right across a list `a`, recursively slamming a
808binary gate `b` with an element from the `list` and an accumulator,
809producing the final value of the accumulator.
811(To "slam" means to call a gate and give it a sample/samples. In this instance,
812`a` is the list of samples that are given to the gate b.)
814The initial value of the accumulator is `b`'s second argument (`+<+`). This can
815occasionally produce undesired behavior (see examples). If you need more control
816over the initial value, try making use of `$_` and `|:`, or perhaps
817[`+spin`](#spin) or [`+spun`](#spun).
819#### Accepts
821`a` is a list.
823`b` is a binary gate.
825#### Produces
827The accumulator, which is a noun.
829#### Source
831```hoon
832++ roll
833 ~/ %roll
834 |* [a=(list) b=_=>(~ |=([* *] +<+))]
835 |- ^+ ,.+<+.b
836 ?~ a
837 +<+.b
838 $(a t.a, b b(+<+ (b i.a +<+.b)))
839```
841#### Examples
843```
844> (roll `(list @)`[1 2 3 4 5 ~] add)
845q=15
847> (roll `(list @)`[6 3 1 ~] sub)
848! subtract-underflow
849! exit
851> (roll `(list @)`[1 3 6 ~] sub)
852q=4
853```
855`+mul`'s default sample is 1, so calling `+roll` with `+mul` yields the expected behavior:
857```
858> *mul
861> (roll `(list @)`~[1 2 3 4] mul)
86224
863```
865However, if you build a gate that uses `+mul` like so, the sample defaults to 0
866since that is the bunt of `@`:
868```
869> (roll `(list @)`~[1 2 3 4] |=([a=@ b=@] (mul a b)))
871```
873We can fix this with `|:`:
875```
876> (roll `(list @)`~[1 2 3 4] |:([a=1 b=1] (mul a b)))
87724
878```
879If you check the definition of `+mul`, you'll see that it also utilizes this
880pattern.
882We can check explicitly what sequence of operations `+roll` performs like this:
883```
884> =f |: [l='e_l' r='e_r']
885 ^- @t
886 :((cury cat 3) '(' l '*' r ')')
887> (roll "abcde" f)
888'(e*(d*(c*(b*(a*e_r)))))
889```
890This is in contrast to what one might expect:
891```
892> =foldl
893 |* [l=(list) f=$-([* *] *)]
894 ^- f
895 ?~ l +<-.f
896 %= $
897 +<-.f (f +<-.f i.l)
898 l t.l
899 ==
900> (foldl "abcde" f)
901'(((((e_l*a)*b)*c)*d)*e)'
902```
904---
906## `++scag`
908Prefix
910Accepts an atom `a` and `list` `b`, producing the first `a` elements of
911the front of the list.
913#### Accepts
915`a` is an atom.
917`b` is a list.
919#### Produces
921A list of the same type as `b`.
923#### Source
925```hoon
926++ scag
927 ~/ %scag
928 |* [a=@ b=(list)]
929 |- ^+ b
930 ?: |(?=(~ b) =(0 a)) ~
931 [i.b $(b t.b, a (dec a))]
932```
934#### Examples
936```
937> (scag 2 `(list @)`[1 2 3 4 ~])
938[i=1 t=~[2]]
939```
941```
942> (scag 10 `(list @)`[1 2 3 4 ~])
943[i=1 t=~[2 3 4]]
944```
946---
948## `++skid`
950Separate
952Separates a `list` `a` into two lists - Those elements of `a` who produce
953true when slammed to gate `b` and those who produce `%.n`.
955(To "slam" means to call a gate and give it a sample/samples. In this instance,
956`a` is the list of samples that are given to the gate `b`.)
958#### Accepts
960`a` is a list.
962`b` is a gate that accepts one argument and produces a flag.
964#### Produces
966A cell of two lists.
968#### Source
970```hoon
971++ skid
972 ~/ %skid
973 |* [a=(list) b=$-(* ?)]
974 |- ^+ [p=a q=a]
975 ?~ a [~ ~]
976 =+ c=$(a t.a)
977 ?:((b i.a) [[i.a p.c] q.c] [p.c [i.a q.c]])
978```
980#### Examples
982```
983> =a |=(a=@ (gth a 1))
984> (skid `(list @)`[0 1 2 3 ~] a)
985[p=[i=2 t=~[3]] q=[i=0 t=~[1]]]
986```
988---
990## `++skim`
992Suffix
994Cycles through the members of a list `a`, passing them to a gate `b` and
995producing a list of all of the members that produce `%.y`. Inverse of
996`skip`.
998#### Accepts
1000`a` is a list.
1002`b` is a gate that accepts one argument and produces a boolean.
1004#### Produces
1006A list.
1008#### Source
1010```hoon
1011++ skim
1012 ~/ %skim
1013 |* [a=(list) b=$-(* ?)]
1014 |-
1015 ^+ a
1016 ?~ a ~
1017 ?:((b i.a) [i.a $(a t.a)] $(a t.a))
1018```
1020#### Examples
1022```
1023> =a |=(a=@ (gth a 1))
1024> (skim `(list @)`[0 1 2 3 ~] a)
1025[i=2 t=~[3]]
1026```
1028---
1030## `++skip`
1032Except
1034Cycles through the members of `list` `a`, passing them to a gate `b`.
1035Produces a list of all of the members that produce `%.n`. Inverse of
1036`skim`.
1038#### Accepts
1040`a` is a list.
1042`b` is a gate that accepts one argument and produces a flag.
1044#### Produces
1046A list of the same type as `a`.
1048#### Source
1050```hoon
1051++ skip
1052 ~/ %skip
1053 |* [a=(list) b=$-(* ?)]
1054 |-
1055 ^+ a
1056 ?~ a ~
1057 ?:((b i.a) $(a t.a) [i.a $(a t.a)])
1058```
1060#### Examples
1062```
1063> =a |=(a=@ (gth a 1))
1064> (skip `(l)`[0 1 2 3 ~]) a)
1065[i=0 t=[i=1 t=~]]
1066```
1068---
1070## `++slag`
1072Suffix
1074Accepts an atom `a` and list `b`, producing the remaining elements from
1075`b` starting at `a`.
1077#### Accepts
1079`a` is an atom.
1081`b` is a list.
1083#### Produces
1085A list of the same type as `b`.
1087#### Source
1089```hoon
1090++ slag
1091 ~/ %slag
1092 |* [a=@ b=(list)]
1093 |- ^+ b
1094 ?: =(0 a) b
1095 ?~ b ~
1096 $(b t.b, a (dec a))
1097```
1099#### Examples
1101```
1102> (slag 2 (limo [1 2 3 4 ~]))
1103[i=3 t=[i=4 t=~]]
1104> (slag 1 (limo [1 2 3 4 ~]))
1105[i=2 t=[i=3 t=[i=4 t=~]]]
1106```
1108---
1110## `++snag`
1112Index
1114Accepts an atom `a` and a `++list` `b`, producing the element at the index
1115of `a`and failing if the list is null. Lists are 0-indexed.
1117#### Accepts
1119`a` is an atom.
1121`b` is a list.
1123#### Produces
1125Produces an element of `b`, or crashes if no element exists at that index.
1127#### Source
1129```hoon
1130++ snag
1131 ~/ %snag
1132 |* [a=@ b=(list)]
1133 |- ^+ ?>(?=(^ b) i.b)
1134 ?~ b
1135 ~_ leaf+"snag-fail"
1136 !!
1137 ?: =(0 a) i.b
1138 $(b t.b, a (dec a))
1139```
1141#### Examples
1143```
1144> (snag 2 "asdf")
1145'd'
1146```
1148```
1149> (snag 0 `(list @ud)`~[1 2 3 4])
1151```
1153---
1155## `++snap`
1157Replace item at index
1159Accepts a `list` `a`, an atom `b`, and a noun `c`, producing the list of `a` with the item at index `b` replaced with `c`.
1161#### Accepts
1163`a` is a list.
1165`b` is a atom.
1167`c` is a noun.
1169#### Produces
1171the list of `a` with the item at index `b` replaced with `c`.
1173#### Source
1175```hoon
1176++ snap
1177 ~/ %snap
1178 |* [a=(list) b=@ c=*]
1179 ^+ a
1180 (weld (scag b a) [c (slag +(b) a)])
1181```
1183#### Examples
1185```
1186> (snap (limo ~[2 3 4]) 1 11)
1187~[2 11 4]
1188```
1190---
1192## `++snip`
1194Drop tail off list
1196Removes the last element from list `a`.
1198#### Accepts
1200`a` is a `list`.
1202#### Produces
1204A `list`.
1206#### Source
1208```hoon
1209++ snip
1210 ~/ %snip
1211 |* a=(list)
1212 ^+ a
1213 ?~ a ~
1214 ?: =(~ t.a) ~
1215 [i.a $(a t.a)]
1216```
1218#### Examples
1220```
1221> `tape`(snip "foobar")
1222"fooba"
1223```
1225```
1226> (snip ~)
1228```
1230---
1232## `++snoc`
1234Append
1236Accepts a `++list` `a` and a noun `b`, producing the list of `b` appended to `a`.
1238#### Accepts
1240`a` is a list.
1242`b` is a noun.
1244#### Produces
1246Produces a list of `b` appended to `a`.
1248#### Source
1250```hoon
1251++ snoc
1252 |* [a=(list) b=*]
1253 (weld a ^+(a [b]~))
1254```
1256#### Examples
1258```
1259> `tape`(zing (snoc `(list tape)`~["a" "bc" "def"] "g"))
1260"abcdefg"
1261> (snoc `(list @ud)`~[1 2 3] 4)
1262~[1 2 3 4]
1263```
1265---
1267## `++sort`
1269Quicksort
1271Quicksort: accepts a `++list` `a` and a gate `b` which accepts two nouns and
1272produces a flag. `++sort` then produces a list of the elements of `a`,
1273sorted according to `b`.
1275#### Accepts
1277`a` is a list.
1279`b` is a gate that accepts two nouns and produces a boolean.
1281#### Produces
1283A list
1285#### Source
1287```hoon
1288++ sort !.
1289 ~/ %sort
1290 |* [a=(list) b=$-([* *] ?)]
1291 => .(a ^.(homo a))
1292 |- ^+ a
1293 ?~ a ~
1294 =+ s=(skid t.a |:(c=i.a (b c i.a)))
1295 %+ weld
1296 $(a p.s)
1297 ^+ t.a
1298 [i.a $(a q.s)]
1299```
1301#### Examples
1303```
1304> (sort `(list @)`[0 1 2 3 ~] gth)
1305~[3 2 1 0]
1306```
1308---
1310## `++spin` {#spin}
1312Gate to list, with state
1314Accepts a `++list` `a`, some state `b`, and a gate `c`. `c` is called with
1315a tuple -- the head is an element of `a` and the tail is the state `b`, and
1316should produce a tuple of the transformed element and the (potentially
1317modified) state `b`. Produces a pair where the first element is a list of the
1318transformed elements of `a`, and the second element is the final value of `b`.
1320#### Accepts
1322`a` is a `++list`.
1324`b` is a noun.
1326`c` is a gate.
1328#### Produces
1330A pair of a list and a noun.
1332#### Source
1334```hoon
1335++ spin
1336 ~/ %spin
1337 |* [a=(list) b=* c=_|=(^ [** +<+])]
1338 => .(c `$-([_?>(?=(^ a) i.a) _b] [_-:(c) _b])`c)
1339 =/ acc=(list _-:(c)) ~
1340 |- ^- (pair _acc _b)
1341 ?~ a
1342 [(flop acc) b]
1343 =^ res b (c i.a b)
1344 $(acc [res acc], a t.a)
1345```
1347#### Examples
1349```
1350> %^ spin (limo ~[4 5 6]) :: Trivial example -- does nothing with the state
1352 |=([n=@ a=@] [n a])
1353[p=~[4 5 6] q=0]
1354```
1356```
1357> %^ spin (limo ~[4 5 6]) :: Form a pair with `p` as the index and `q` as the list element
1359 |=([n=@ a=@] [`(pair)`[a n] +(a)])
1360[p=~[[p=0 q=4] [p=1 q=5] [p=2 q=6]] q=3]
1361```
1363```
1364> %^ spin (reap 10 0) :: Create 10 random numbers less than `10`
1365 ~(. og eny)
1366 |=([n=@ rng=_og] (rads:rng 10))
1367[p=~[7 8 6 0 1 5 4 7 9 3] q=<4.rvi {a/@uvJ <51.qyl 129.pdd 41.mac 1.ane $141>}>]
1368```
1370#### Discussion
1372`(~(rads og eny) 2)` creates a random number less than `2`, seeding the RNG
1373with entropy (`eny`). The head of the product is the random number, the tail
1374is the continuation of the RNG.
1376---
1378## `++spun` {#spun}
1380Gate to list, with state
1382Accepts a `list` `a` and a gate `b`. `c` is internal state, initially
1383derived by **bunting** the tail of the sample of gate `b`, instead of
1384being passed in explicitly as in `++spin`. Produces a list with the
1385gate applied to each element of the original list. `b` is called with a tuple --
1386the head is an element of `a` and the tail is the state `c`, and should
1387produce a tuple of the transformed element and the (potentially modified)
1388state `c`.
1390#### Accepts
1392`a` is a `++list`.
1394`b` is a gate.
1396#### Produces
1398A list.
1400#### Source
1402```hoon
1403++ spun
1404 ~/ %spun
1405 |* [a=(list) b=_|=(^ [** +<+])]
1406 p:(spin a +<+.b b)
1407```
1409#### Examples
1411```
1412> %+ spun (limo ~[4 5 6]) :: `p` as the index and `q` as the list element
1413 |=([n=@ a=@] [`(pair)`[a n] +(a)])
1414~[[p=0 q=4] [p=1 q=5] [p=2 q=6]]
1415```
1417```
1418> =l (limo ~[7 8 9])
1419> %+ spun (limo ~[4 5 6]) :: joins two lists into a list of pairs
1420 |=([n=@ a=@] [`(pair)`[(snag a l) n] +(a)])
1421~[[p=7 q=4] [p=8 q=5] [p=9 q=6]]
1422```
1424---
1426## `++swag`
1428Infix
1430Similar to `substr` in Javascript: extracts a string infix, beginning at
1431inclusive index `a`, producing `b` number of characters.
1433#### Accepts
1435`a` is an atom.
1437`b` is an atom.
1439`c` is a list.
1441#### Produces
1443A list of the same type as `c`.
1445#### Source
1447```hoon
1448++ swag
1449 |* [[a=@ b=@] c=(list)]
1450 (scag +<-> (slag +<-< c))
1451```
1453#### Examples
1455```
1456> (swag [2 5] "roly poly")
1457"ly po"
1458```
1460```
1461> (swag [2 2] (limo [1 2 3 4 ~]))
1462[i=3 t=[i=4 t=~]]
1463```
1465---
1467## `++turn`
1469Gate to list
1471Accepts a `++list` `a` and a gate `b`. Produces a list with the gate applied
1472to each element of the original list.
1474#### Accepts
1476`a` is a list.
1478`b` is a gate.
1480#### Produces
1482A list.
1484#### Source
1486```hoon
1487++ turn
1488 ~/ %turn
1489 |* [a=(list) b=gate]
1490 => .(a (homo a))
1491 ^- (list _?>(?=(^ a) (b i.a)))
1492 |-
1493 ?~ a ~
1494 [i=(b i.a) t=$(a t.a)]
1495```
1497#### Examples
1499```
1500> (turn (limo [104 111 111 110 ~]) @t)
1501<|h o o n|>
1502```
1504```
1505> =a |=(a=@ (add a 4))
1506> (turn (limo [1 2 3 4 ~]) a)
1507~[5 6 7 8]
1508```
1510#### Discussion
1512`turn` is Hoon's version of 'map' in Haskell.
1514---
1516## `++weld`
1518Concatenate
1520Concatenate two `++list`s `a` and `b`.
1522#### Accepts
1524`a` and `b` are lists.
1526#### Source
1528```hoon
1529++ weld
1530 ~/ %weld
1531 |* [a=(list) b=(list)]
1532 => .(a ^.(homo a), b ^.(homo b))
1533 |- ^+ b
1534 ?~ a b
1535 [i.a $(a t.a)]
1536```
1538#### Examples
1540```
1541> (weld "urb" "it")
1542"urbit"
1543```
1545```
1546> (weld (limo [1 2 ~]) (limo [3 4 ~]))
1547~[1 2 3 4]
1548```
1550---
1552## `++welp`
1554Perfect weld
1556Concatenate two `++list`s `a` and `b` without losing their type information
1557to homogenization.
1559#### Accepts
1561`a` is a list.
1563`b` is a list.
1565#### Produces
1567A list.
1569#### Source
1571```hoon
1572++ welp
1573 ~/ %welp
1574 =| [* *]
1575 |@
1576 ++ $
1577 ?~ +<-
1578 +<-(. +<+)
1579 +<-(+ $(+<- +<->))
1580 --
1581```
1583#### Examples
1585```
1586> (welp "foo" "bar")
1587"foobar"
1588```
1590```
1591> (welp ~[60 61 62] ~[%a %b %c])
1592[60 61 62 %a %b %c ~]
1594> ? (welp ~[60 61 62] ~[%a %b %c])
1595 {@ud @ud @ud $a $b $c $~}
1596[60 61 62 %a %b %c ~]
1597```
1599```
1600> (welp [sa+1 so+2 ~] si=3)
1601[[%sa 1] [%so 2] si=3]
1602```
1604---
1606## `++zing`
1608Turns a `++list` of lists into a single list by promoting the elements of
1609each sublist into the higher.
1611#### Accepts
1613A list of lists.
1615#### Produces
1617A list.
1619#### Source
1621```hoon
1622++ zing
1623 ~/ %zing
1624 =| *
1625 |@
1626 ++ $
1627 ?~ +<
1628 +<
1629 (welp +<- $(+< +<+))
1630 --
1631```
1633#### Examples
1635```
1636> (zing (limo [(limo ['a' 'b' 'c' ~]) (limo ['e' 'f' 'g' ~]) (limo ['h' 'i' 'j' ~]) ~]))
1637~['a' 'b' 'c' 'e' 'f' 'g' 'h' 'i' 'j']
1638```
1640```
1641> (zing (limo [(limo [1 'a' 2 'b' ~]) (limo [3 'c' 4 'd' ~]) ~]))
1642~[1 97 2 98 3 99 4 100]
1643```
1645---