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) |
1) the A.find function gets a value and a unidimensional array, |
||
then retuns the item matching the value else -1 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
{A.find.r :val :arr {- {A.length :arr} 1} 0 {A.new}}}} |
|||
⚫ | |||
{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.find 21 {A}} |
|||
⚫ | |||
-> -1 // not found |
|||
⚫ | |||
⚫ | |||
-> 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, |
|||
⚫ | |||
and diplays the row containing the value if it exists else displays "the value was not found". |
|||
⚫ | |||
3) a function scanning a row and returning true as soon as the value is 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 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
then {find.row :val {A.new} true} |
|||
⚫ | |||
⚫ | |||
3.1) the rn function returns a random integer between 0 and n |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
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} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
3.2) creating a bidimensional array containing random integers between 0 and 20 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
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> |
||