- XLogo library of procedures
These procedures can be copied and pasted into the Editor window as usual. Most do not produce any graphics. To test type Test . The results will show in the History window.
Download library of list procedures .
Flatten
Reduce a list which may contain other lists to a single list of items. eg Flatten [ a b [ c [ d e ] [f] [ [g h] ] i ] ] will result [ a b c d e f g h i ].
To
Flatten
:
myList
# flatten list to single depth
If Empty?
:
myList
[Output [] ]
If List? First
:
myList
[
Output (Se
Flatten
First
:
myList
Flatten
ButFirst
:
myList
)]
Output FPut First
:
myList
Flatten
ButFirst
:
myList
End
To
Test
# test procedure
Make "
myList
[ a b [ c [ d e ] [f] [ [g h] ] i ] ]
Print (Sentence "Flattened
:
myList
"is
Flatten
:
myList
End
Highest
(also see
Lowest
)
Return the highest value within the list.
To
Highest
:
myList
# return highest value
LocalMake "
Highest
First
:
myList
ForEach "
item
:
myList
[
If Before?
:
Highest
:
item
[ Make "
Highest
:
item
] ]
Output
:
Highest
End
To
Test
# test procedure
Make "
myList
[1 7 -11 3 8 5]
Print (Sentence "[Highest item of]
:
myList
"is
Highest
:
myList
End
Lowest
(also see
Highest
)
Return the lowest value within the list.
To
Lowest
:
myList
# return lowest value
LocalMake "
Lowest
First
:
myList
ForEach "
item
:
myList
[
If Before?
:
item
:
Lowest
[Make "
Lowest
:
item
] ]
Output
:
Lowest
End
To
Test
# test procedure
Make "
myList
[1 7 -11 3 8 5]
Print (Sentence "[Lowest item of]
:
myList
"is
Lowest
:
myList
End
Mean
Return the mean or average value of a list of values.
To
Mean
:
myList
# return average value
LocalMake "
Total
0
ForEach "
Item
:
myList
[
LocalMake "
Total
:
Total
+
:
Item
]
Output
:
Total
/ Count
:
myList
End
To
Test
# test procedure
Make "
myList
[1 7 -11 3 8 5]
Print (Sentence "[Mean value of]
:
myList
"is
Mean
:
myList
End
Rotate Left
(also see
Rotate Right
)
Return list with items rotated one place to the left.
To
RoL
:
myList
# rotate list items left
LocalMake "
FirstItem
First
:
myList
Output AddItem ButFirst
:
myList
Count
:
myList
:
FirstItem
End
To
Test
# test procedure
Make "
myList
[1 2 3 4 5]
Print (Sentence "Rotate
:
myList
[left is]
RoL
:
myList
End
Rotate Right
(also see
Rotate Left
)
Return list with items rotated one place to the right.
To
RoR
:
myList
# rotate list items right
LocalMake "
LastItem
Last
:
myList
Output AddItem ButLast
:
myList
1
:
LastItem
End
To
Test
# test procedure
Make "
myList
[1 2 3 4 5]
Print (Sentence "Rotate
:
myList
[right is]
RoR
:
myList
End
Shuffle
(also see
Sort
)
This procedure shuffles a list.
To
Shuffle
:
myList
# randomly shuffle list order
LocalMake "
Shuffled
[ ]
Repeat Count
:
myList
[
LocalMake "
Select
Pick
:
myList
Make "
myList
Remove
:
Select
:
myList
LocalMake "
Shuffled
LPut
:
Select
:
Shuffled
]
Output
:
Shuffled
End
Sort
(also see
Shuffle
)
This procedure sorts a list of items into alphabetical or numeric order. It uses the bubble sort method. There are more efficient methods if the list is long.
To
Sort
:
myList
# sort list into alphabetical order
LocalMake "
Num
Count
:
myList
Repeat (
:
Num
-1) [
For (List "
J
1 (
:
Num
-1)) [
LocalMake "
FirstItem
Item
:
J
:
myList
LocalMake "
SecondItem
Item (
:
J
+1)
:
myList
If Before?
:
SecondItem
:
FirstItem
[
#swop the two items
Make "
myList
SetItem
:
myList
:
J
:
SecondItem
Make "
myList
SetItem
:
myList
(
:
J
+1)
:
FirstItem
] ] ]
Output
:
myList
End
Take
(Pick and Remove)
Warning
This is a bad example of code writing and serves as an example of how not to do it.
Similar to primitive
Pick
, except that the chosen item is removed from the list. Note that all instances are removed, in the same way as primitive
Remove
.
To
Take
:
myLst
# return one item taken away randomly from list
LocalMake "
ThisItem
Pick Thing
:
myLst
Make
:
myLst
Remove
:
ThisItem
Thing
:
myLst
Output
:
ThisItem
End
To
Test
# test procedure
Make "
myList
[1 2 3 4 5]
Repeat Count
:
myList
[
Print (Sentence
:
myList
"take (
Take
"
myList
) "leaves
:
myList
) ]
End