Loops/Nested: Difference between revisions

Content added Content deleted
No edit summary
No edit summary
Line 1,921: Line 1,921:
=={{header|Lambdatalk}}==
=={{header|Lambdatalk}}==
<lang scheme>
<lang scheme>
1) a function returning random numbers in [0,20]
1) the A.find function gets a value and a unidimensional array,
then retuns the item matching the value else -1


{def A.find
{def rn {lambda {} {round {* 20 {random}}}}}
{def A.find.r
-> rn
{lambda {:val :arr :n :i :acc}
{if {> :i :n}
then -1
else {if {= :val {A.get :i :arr}}
then :i
else {A.find.r :val :arr :n {+ :i 1} {A.addlast! :i :acc}}}}}}
{lambda {:val :arr}
{A.find.r :val :arr {- {A.length :arr} 1} 0 {A.new}}}}
-> A.find


{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}}
{A.new {rn} {rn} {rn} {rn}}
-> -1 // not 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 AA.find
3) a function scanning a row and returning true as soon as the value is found
{def AA.find.r
{lambda {:val :arr :n :i}
{if {> :i :n}
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.get :i :arr} {AA.find.r :val :arr :n {+ :i 1}} }}}}
{lambda {:val :arr}
{AA.find.r :val :arr {- {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


4) the main function returning the array's items until the value is found, or not
3.1) the rn function returns a random integer between 0 and n
{def rn {lambda {:n} {round {* :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 = [[9,4,10,14,1],[4,12,7,18,13],[7,13,19,12,11],[18,4,2,14,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>
</lang>