Skip to content

Commit e96264a

Browse files
committed
update
1 parent 1b48f4a commit e96264a

File tree

1 file changed

+91
-2
lines changed

1 file changed

+91
-2
lines changed

README.md

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,95 @@ undefined
884884
[3,2,1]
885885
```
886886

887-
#Loop Functions
887+
##Loop Functions
888888

889-
Unlike other functional programming languages, RoyalScript incorporates
889+
Unlike most other functional programming languages, RoyalScript has loops, a conditional loop and a for loop. These exist to add more flexibility in writing RoyalScript than purely using recursion as the primary tool of computation.
890+
891+
###loop(bool\_exp, call\_exp)
892+
893+
The *loop()* function in RoyalScript repeatedly calls a single statement while the boolean expression, also called a condition evaluates to true. The call expression, like that in *!@()* can be extended with the *do()* function.
894+
895+
Here is a small, RoyalScript program that uses a loop to sum the values in a list. This is a compiled program, as opposed to the REPL used in the previous examples.
896+
897+
```
898+
=(l, list(1, 2, 3)),
899+
=(i, 0),
900+
=(total, 0),
901+
902+
loop(<(i, len(l)),
903+
do(
904+
=(total, +(total, get(l, i))),
905+
=(i, +(i, 1))
906+
)
907+
),
908+
do(total)
909+
;The result is 6;
910+
```
911+
912+
###for(list, function_proc)
913+
914+
The *for()* function in RoyalScript allows one to loop over lists, by calling some function or proc once on each item in the list.
915+
916+
RoyalScript uses a for every type of loop as opposed to traditional C-style type for loops because a functional programming language needs to be able to use functions more than just blocks of code in different arrays of a program.
917+
918+
This is a program that loops over a range and inserts the numbers greater than 3 in reverse order in a new list.
919+
920+
```
921+
=(r, list()),
922+
923+
for(range(0, 10),
924+
!@( elem,
925+
do(
926+
?(>(elem, 3), insert(r, 0, elem))
927+
)
928+
)
929+
),
930+
do(r)
931+
;[9,8,7,6,5,4];
932+
```
933+
934+
You can also use functions you define yourself in for loops, which will be explained in the function section.
935+
936+
Something important to understand is that unless directly accessing the name of a list, the for loop cannot modify the list it is iterating over. See this example:
937+
938+
```
939+
=(lst, list(1, 2, 3)),
940+
for(lst,
941+
!@(elem, +(elem, 2))
942+
)
943+
,do(lst)
944+
;[1,2,3];
945+
```
946+
947+
##List Construction and Comprehension
948+
949+
In previous examples, lists were constructed in a mutable fashion with loops or functions that had to be specified to filter and/or modify a list. In python, a popular object oriented programming language, it is possible to construct lists through the use of list comprehensions. RoyalScript implements a very similar concept through the *map()* and *filter()* functions, with even more powerful options if you chain them together.
950+
951+
###map(list, function_proc)
952+
953+
The *map()* function takes a list and a function or proc(cannot use !@, must return value), and returns a new list that has every element in the old list applied to it. The old list that is called with map is not changed.
954+
955+
Here is an example using a single parameter proc that gets the squares from a range:
956+
957+
```
958+
map(range(0, 10),
959+
@(x, **(x, 2))
960+
)
961+
;[0,1,4,9,16,25,36,49,64,81];
962+
```
963+
A double parameter proc, *@@()* can also be used for this:
964+
965+
```
966+
map(range(0, 10),
967+
@@(a, b, list(a, b))
968+
)
969+
;[[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6],[7,7],[8,8],[9,9]];
970+
```
971+
972+
###filter(list, function_proc -> Boolean)
973+
974+
The *filter()* function in RoyalScript takes a list and a function or proc that takes one argument and returns a boolean value. It goes through a list, and checks if eahc value returns true or false from the proc or function. It then returns a new list for all the values that made the function or proc return true.
975+
976+
```
977+
978+
```

0 commit comments

Comments
 (0)