Anonymous user
Loops/Nested: Difference between revisions
no edit summary
No edit summary |
No edit summary |
||
Line 1,921:
=={{header|Lambdatalk}}==
<lang scheme>
1)
then retuns the item matching the value else -1
{def A.find▼
{def rn {lambda {} {round {* 20 {random}}}}} ▼
-> rn ▼
{lambda {:val :arr}▼
{A.find.r :val :arr {- {A.length :arr} 1} 0 {A.new}}}}
{def A {A.new {S.serie 0 20}}}
-> 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}}
-> 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} {rn}}▼
{A.new {rn} {rn} {rn} {rn}}}}▼
2) the AA.find function gets a value and a bidimensional 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.r
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}
{lambda {:val :arr}
3) testing
▲{def find.row
▲ {lambda {:val :arr :flag}
▲ {if {A.empty? :arr}
▲ then :flag
▲ else {if {= {A.first :arr} :val}
▲ else {find.row :val {A.rest :arr} :flag}} }}}
▲-> find.row
▲{def find
▲ {def find.rec
▲ {lambda {:val :arr :flag}
▲ {if {A.empty? :arr}
▲ then {if :flag
▲ then :val was found!
▲ 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
▲ {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 = [[
3.3) calling with a value which can be in the array
{AA.find 12 {AA}}
->
[9,4,10,14,1]
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>
|