Loops/Nested: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 1,921:
=={{header|Lambdatalk}}==
<lang scheme>
1) athe A.find function returninggets randoma numbersvalue inand [0a unidimensional array,20]
then retuns the item matching the value else -1
 
{def A.find
{def rn {lambda {} {round {* 20 {random}}}}}
{def A.find.recr
-> rn
{lambda {:val :arr :flagn :i :acc}
{if {A.empty?> :arri :n}
then :flag-1
else {if {= :val {A.firstget :arr}i :valarr}}
then {if :flagi
else {A.find.rowr :val :arr :n {+ :i 1} {A.restaddlast! :arr}i :flagacc}}} }}}
{lambda {:val :arr}
{A.find.r :val :arr {- {A.length :arr} 1} 0 {A.new}}}}
-> findA.rowfind
 
{def A {A.new {S.serie 0 20}}}
2) a random array
-> A = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
 
{A.find 12 {A}}
{def random_array
-> 12 // the index
{A.new {A.new {rn} {rn} {rn} {rn}}
{A.find 21 {A}}
{A.new {rn} {rn} {rn} {rn}}
-> -1 {A.new {rn} {rn}// {rn}not {rn}}found
{A.new {rn} {rn} {rn} {rn}}
{A.new {rn} {rn} {rn} {rn}}}}
-> random_array
 
2) the AA.find function gets a value and a bidimensional array,
{random_array}
then returns the sequence of rows until the row containing the value,
-> [[12,19,19,18],[14,13,5,4],[2,6,16,12],[11,12,9,8],[18,8,17,9]]
and diplays the row containing the value if it exists else displays "the value was not found".
 
{def findAA.rowfind
3) a function scanning a row and returning true as soon as the value is found
{def AA.find.r
{lambda {:val :arr :flagn :i}
{if {A.empty?> :i :arrn}
then {br}:val was not found!
else {if {not {= {A.find :val {A.get :i :arr}} -1}} // call the A.find function on each row
then {br}:val was found in {A.get :i :arr}
else {br}{A.firstget :i :arr} {AA.find.recr :val :arr :n {A.rest+ :arr}i :flag1}} }}}}
{lambda {:val :arr}
{AA.find.recr :val :arr false}{- {A.length :arr} 1} 0}}}
-> AA.find
 
3) testing
{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
 
43.1) the mainrn function returningreturns thea array's items until therandom valueinteger isbetween found,0 orand notn
{def rn {lambda {:n} {round {* 20:n {random}}}}}
 
-> rn
{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
 
3.2) creating a bidimensional array containing random integers between 0 and 20
{def AA {A.new {A.new {rn 20} {rn 20} {rn 20} {rn 20} {rn 20}}
{A.new {rn 20} {rn 20} {rn 20} {rn 20} {rn 20}}
{A.new {rn 20} {rn 20} {rn 20} {rn 20} {rn 20}}
{A.new {rn 20} {rn 20} {rn 20} {rn 20} {rn 20}}}}
-> AA = [[129,194,1910,1814,1],[144,1312,57,418,13],[27,613,1619,12],[11,12,9,8],[18,84,172,914,15]]
 
3.3) calling with a value which can be in the array
{find 5 {random_array}}
{AA.find 12 {AA}}
-> [12,19,19,18] [14,13,5,4] 5 was found!
->
{find 15 {random_array}}
[9,4,10,14,1]
-> [12,19,19,18] [14,13,5,4] [2,6,16,12] [11,12,9,8] [18,8,17,9] 15 was not found.
12 was found in [4,12,7,18,13]
 
3.4) calling with a value outside of the array
{AA.find 21 {AA}}
->
[9,4,10,14,1]
[4,12,7,18,13]
[7,13,19,12,11]
[18,4,2,14,15]
21 was not found
</lang>