Jump to content

Loops/Nested: Difference between revisions

1,647 bytes added ,  4 years ago
no edit summary
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
No edit summary
Line 1,918:
row 1: 6 2 6 1 11 10 2 8 1 14
row 2: 3 6 4 6 10 2 10 20</pre>
 
=={{header|Lambdatalk}}==
<lang scheme>
1) a function returning random numbers in [0,20]
 
{def rn {lambda {} {round {* 20 {random}}}}}
-> rn
 
2) a random array
 
{def random_array
{A.new {A.new {rn} {rn} {rn} {rn}}
{A.new {rn} {rn} {rn} {rn}}
{A.new {rn} {rn} {rn} {rn}}
{A.new {rn} {rn} {rn} {rn}}
{A.new {rn} {rn} {rn} {rn}}}}
-> random_array
 
{random_array}
-> [[12,19,19,18],[14,13,5,4],[2,6,16,12],[11,12,9,8],[18,8,17,9]]
 
3) a function scanning a row and returning true as soon as the value is found
 
{def find.row
{lambda {:val :arr :flag}
{if {A.empty? :arr}
then :flag
else {if {= {A.first :arr} :val}
then {find.row :val {A.new} true}
else {find.row :val {A.rest :arr} :flag}} }}}
-> find.row
 
4) the main function returning the array's items until the value is found, or not
 
{def find
{def find.rec
{lambda {:val :arr :flag}
{if {A.empty? :arr}
then {if :flag
then :val was found!
else :val was not found.}
else {let { {:val :val} {:arr :arr} {:flag :flag} }
{if {find.row :val {A.first :arr} false}
then {A.first :arr} {find.rec :val {A.new} true}
else {A.first :arr} {find.rec :val {A.rest :arr} :flag}} }}}}
{lambda {:val :arr}
{find.rec :val :arr false}}}
-> find
 
 
{find 5 {random_array}}
-> [12,19,19,18] [14,13,5,4] 5 was found!
{find 15 {random_array}}
-> [12,19,19,18] [14,13,5,4] [2,6,16,12] [11,12,9,8] [18,8,17,9] 15 was not found.
 
</lang>
 
=={{header|Lasso}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.