Basic Array Construction

Category:
Arrays, Hash Tables and Dictionaries
Description: This sample demonstrates basic array construction.
Code:
let BasicConstruction() =

let emptyA = [||]

let smallNumsA = [|1; 2; 3|]

let emptyA2: int array = Array.zeroCreate 5

printfn "emptyA = %A" emptyA
printfn "\nsmallNumsA = %A" smallNumsA
printfn "\nemptyA2 = %A" emptyA2

Execution Result:
emptyA = [||]

smallNumsA = [|1; 2; 3|]

emptyA2 = [|0; 0; 0; 0; 0|]

Basic Array Construction 2

Category:
Arrays, Hash Tables and Dictionaries
Description: This sample demonstrates more basic array construction.
Code:
let BasicConstruction2() =

// Create an array using a range expression
// general form: let = [|startExpr..[stepExpr]..endExpr|]
// where [stepExpr] is optional and has default value of 1
let arrRngExp = [|0..5|]

let arrRngExp2 = [|1..2..9|]

// Create an array using a function based on index
let squaresByInit = Array.init 5 (fun i -> (i, i*i))

// Create an array using array comprehension
let squaresByComp = [| for i in 0..5 do yield (i, i*i) |]

printfn "arrRngExp = %A" arrRngExp
printfn "\narrRngExp2 = %A" arrRngExp2
printfn "\nsquaresByInit = %A" squaresByInit
printfn "\nsquaresByComp = %A" squaresByComp

// Comprehensions can contain arbitrary code
let arbitraryComp =
[| for i in 1..10 do
if i <> 7 then
printfn "%d" i
yield i
printfn "did thru 10, skipped 7"
yield 100 |]


printfn "\narbitraryComp = %A" arbitraryComp

Execution Result:
arrRngExp = [|0; 1; 2; 3; 4; 5|]

arrRngExp2 = [|1; 3; 5; 7; 9|]

squaresByInit = [|(0, 0); (1, 1); (2, 4); (3, 9); (4, 16)|]

squaresByComp = [|(0, 0); (1, 1); (2, 4); (3, 9); (4, 16); (5, 25)|]
1
2
3
4
5
6
8
9
10
did thru 10, skipped 7

arbitraryComp = [|1; 2; 3; 4; 5; 6; 8; 9; 10; 100|]

Using arrays

Category:
Arrays, Hash Tables and Dictionaries
Description:
Code:
let SampleArray1() =
let size = 1000
let arr = Array.create size 0
for i = 1 to size - 1 do
arr.[i] <- i + arr.[i-1]
for i = 1 to size - 1 do
printfn "arr.[%4d] = %d" i arr.[i]

Execution Result:
arr.[   1] = 1
arr.[ 2] = 3
arr.[ 3] = 6
arr.[ 4] = 10
arr.[ 5] = 15
arr.[ 6] = 21
arr.[ 7] = 28
arr.[ 8] = 36
arr.[ 9] = 45
arr.[ 10] = 55
arr.[ 11] = 66
arr.[ 12] = 78
arr.[ 13] = 91
arr.[ 14] = 105
arr.[ 15] = 120
arr.[ 16] = 136
arr.[ 17] = 153
arr.[ 18] = 171
arr.[ 19] = 190
arr.[ 20] = 210
arr.[ 21] = 231
arr.[ 22] = 253
arr.[ 23] = 276
arr.[ 24] = 300
arr.[ 25] = 325
arr.[ 26] = 351
arr.[ 27] = 378
arr.[ 28] = 406
arr.[ 29] = 435
arr.[ 30] = 465
arr.[ 31] = 496
arr.[ 32] = 528
arr.[ 33] = 561
arr.[ 34] = 595
arr.[ 35] = 630
arr.[ 36] = 666
arr.[ 37] = 703
arr.[ 38] = 741
arr.[ 39] = 780
arr.[ 40] = 820
arr.[ 41] = 861
arr.[ 42] = 903
arr.[ 43] = 946
arr.[ 44] = 990
arr.[ 45] = 1035
arr.[ 46] = 1081
arr.[ 47] = 1128
arr.[ 48] = 1176
arr.[ 49] = 1225
arr.[ 50] = 1275
arr.[ 51] = 1326
arr.[ 52] = 1378
arr.[ 53] = 1431
arr.[ 54] = 1485
arr.[ 55] = 1540
arr.[ 56] = 1596
arr.[ 57] = 1653
arr.[ 58] = 1711
arr.[ 59] = 1770
arr.[ 60] = 1830
arr.[ 61] = 1891
arr.[ 62] = 1953
arr.[ 63] = 2016
arr.[ 64] = 2080
arr.[ 65] = 2145
arr.[ 66] = 2211
arr.[ 67] = 2278
arr.[ 68] = 2346
arr.[ 69] = 2415
arr.[ 70] = 2485
arr.[ 71] = 2556
arr.[ 72] = 2628
arr.[ 73] = 2701
arr.[ 74] = 2775
arr.[ 75] = 2850
arr.[ 76] = 2926
arr.[ 77] = 3003
arr.[ 78] = 3081
arr.[ 79] = 3160
arr.[ 80] = 3240
arr.[ 81] = 3321
arr.[ 82] = 3403
arr.[ 83] = 3486
arr.[ 84] = 3570
arr.[ 85] = 3655
arr.[ 86] = 3741
arr.[ 87] = 3828
arr.[ 88] = 3916
arr.[ 89] = 4005
arr.[ 90] = 4095
arr.[ 91] = 4186
arr.[ 92] = 4278
arr.[ 93] = 4371
arr.[ 94] = 4465
arr.[ 95] = 4560
arr.[ 96] = 4656
arr.[ 97] = 4753
arr.[ 98] = 4851
arr.[ 99] = 4950
arr.[ 100] = 5050
arr.[ 101] = 5151
arr.[ 102] = 5253
arr.[ 103] = 5356
arr.[ 104] = 5460
arr.[ 105] = 5565
arr.[ 106] = 5671
arr.[ 107] = 5778
arr.[ 108] = 5886
arr.[ 109] = 5995
arr.[ 110] = 6105
arr.[ 111] = 6216
arr.[ 112] = 6328
arr.[ 113] = 6441
arr.[ 114] = 6555
arr.[ 115] = 6670
arr.[ 116] = 6786
arr.[ 117] = 6903
arr.[ 118] = 7021
arr.[ 119] = 7140
arr.[ 120] = 7260
arr.[ 121] = 7381
arr.[ 122] = 7503
arr.[ 123] = 7626
arr.[ 124] = 7750
arr.[ 125] = 7875
arr.[ 126] = 8001
arr.[ 127] = 8128
arr.[ 128] = 8256
arr.[ 129] = 8385
arr.[ 130] = 8515
arr.[ 131] = 8646
arr.[ 132] = 8778
arr.[ 133] = 8911
arr.[ 134] = 9045
arr.[ 135] = 9180
arr.[ 136] = 9316
arr.[ 137] = 9453
arr.[ 138] = 9591
arr.[ 139] = 9730
arr.[ 140] = 9870
arr.[ 141] = 10011
arr.[ 142] = 10153
arr.[ 143] = 10296
arr.[ 144] = 10440
arr.[ 145] = 10585
arr.[ 146] = 10731
arr.[ 147] = 10878
arr.[ 148] = 11026
arr.[ 149] = 11175
arr.[ 150] = 11325
arr.[ 151] = 11476
arr.[ 152] = 11628
arr.[ 153] = 11781
arr.[ 154] = 11935
arr.[ 155] = 12090
arr.[ 156] = 12246
arr.[ 157] = 12403
arr.[ 158] = 12561
arr.[ 159] = 12720
arr.[ 160] = 12880
arr.[ 161] = 13041
arr.[ 162] = 13203
arr.[ 163] = 13366
arr.[ 164] = 13530
arr.[ 165] = 13695
arr.[ 166] = 13861
arr.[ 167] = 14028
arr.[ 168] = 14196
arr.[ 169] = 14365
arr.[ 170] = 14535
arr.[ 171] = 14706
arr.[ 172] = 14878
arr.[ 173] = 15051
arr.[ 174] = 15225
arr.[ 175] = 15400
arr.[ 176] = 15576
arr.[ 177] = 15753
arr.[ 178] = 15931
arr.[ 179] = 16110
arr.[ 180] = 16290
arr.[ 181] = 16471
arr.[ 182] = 16653
arr.[ 183] = 16836
arr.[ 184] = 17020
arr.[ 185] = 17205
arr.[ 186] = 17391
arr.[ 187] = 17578
arr.[ 188] = 17766
arr.[ 189] = 17955
arr.[ 190] = 18145
arr.[ 191] = 18336
arr.[ 192] = 18528
arr.[ 193] = 18721
arr.[ 194] = 18915
arr.[ 195] = 19110
arr.[ 196] = 19306
arr.[ 197] = 19503
arr.[ 198] = 19701
arr.[ 199] = 19900
arr.[ 200] = 20100
arr.[ 201] = 20301
arr.[ 202] = 20503
arr.[ 203] = 20706
arr.[ 204] = 20910
arr.[ 205] = 21115
arr.[ 206] = 21321
arr.[ 207] = 21528
arr.[ 208] = 21736
arr.[ 209] = 21945
arr.[ 210] = 22155
arr.[ 211] = 22366
arr.[ 212] = 22578
arr.[ 213] = 22791
arr.[ 214] = 23005
arr.[ 215] = 23220
arr.[ 216] = 23436
arr.[ 217] = 23653
arr.[ 218] = 23871
arr.[ 219] = 24090
arr.[ 220] = 24310
arr.[ 221] = 24531
arr.[ 222] = 24753
arr.[ 223] = 24976
arr.[ 224] = 25200
arr.[ 225] = 25425
arr.[ 226] = 25651
arr.[ 227] = 25878
arr.[ 228] = 26106
arr.[ 229] = 26335
arr.[ 230] = 26565
arr.[ 231] = 26796
arr.[ 232] = 27028
arr.[ 233] = 27261
arr.[ 234] = 27495
arr.[ 235] = 27730
arr.[ 236] = 27966
arr.[ 237] = 28203
arr.[ 238] = 28441
arr.[ 239] = 28680
arr.[ 240] = 28920
arr.[ 241] = 29161
arr.[ 242] = 29403
arr.[ 243] = 29646
arr.[ 244] = 29890
arr.[ 245] = 30135
arr.[ 246] = 30381
arr.[ 247] = 30628
arr.[ 248] = 30876
arr.[ 249] = 31125
arr.[ 250] = 31375
arr.[ 251] = 31626
arr.[ 252] = 31878
arr.[ 253] = 32131
arr.[ 254] = 32385
arr.[ 255] = 32640
arr.[ 256] = 32896
arr.[ 257] = 33153
arr.[ 258] = 33411
arr.[ 259] = 33670
arr.[ 260] = 33930
arr.[ 261] = 34191
arr.[ 262] = 34453
arr.[ 263] = 34716
arr.[ 264] = 34980
arr.[ 265] = 35245
arr.[ 266] = 35511
arr.[ 267] = 35778
arr.[ 268] = 36046
arr.[ 269] = 36315
arr.[ 270] = 36585
arr.[ 271] = 36856
arr.[ 272] = 37128
arr.[ 273] = 37401
arr.[ 274] = 37675
arr.[ 275] = 37950
arr.[ 276] = 38226
arr.[ 277] = 38503
arr.[ 278] = 38781
arr.[ 279] = 39060
arr.[ 280] = 39340
arr.[ 281] = 39621
arr.[ 282] = 39903
arr.[ 283] = 40186
arr.[ 284] = 40470
arr.[ 285] = 40755
arr.[ 286] = 41041
arr.[ 287] = 41328
arr.[ 288] = 41616
arr.[ 289] = 41905
arr.[ 290] = 42195
arr.[ 291] = 42486
arr.[ 292] = 42778
arr.[ 293] = 43071
arr.[ 294] = 43365
arr.[ 295] = 43660
arr.[ 296] = 43956
arr.[ 297] = 44253
arr.[ 298] = 44551
arr.[ 299] = 44850
arr.[ 300] = 45150
arr.[ 301] = 45451
arr.[ 302] = 45753
arr.[ 303] = 46056
arr.[ 304] = 46360
arr.[ 305] = 46665
arr.[ 306] = 46971
arr.[ 307] = 47278
arr.[ 308] = 47586
arr.[ 309] = 47895
arr.[ 310] = 48205
arr.[ 311] = 48516
arr.[ 312] = 48828
arr.[ 313] = 49141
arr.[ 314] = 49455
arr.[ 315] = 49770
arr.[ 316] = 50086
arr.[ 317] = 50403
arr.[ 318] = 50721
arr.[ 319] = 51040
arr.[ 320] = 51360
arr.[ 321] = 51681
arr.[ 322] = 52003
arr.[ 323] = 52326
arr.[ 324] = 52650
arr.[ 325] = 52975
arr.[ 326] = 53301
arr.[ 327] = 53628
arr.[ 328] = 53956
arr.[ 329] = 54285
arr.[ 330] = 54615
arr.[ 331] = 54946
arr.[ 332] = 55278
arr.[ 333] = 55611
arr.[ 334] = 55945
arr.[ 335] = 56280
arr.[ 336] = 56616
arr.[ 337] = 56953
arr.[ 338] = 57291
arr.[ 339] = 57630
arr.[ 340] = 57970
arr.[ 341] = 58311
arr.[ 342] = 58653
arr.[ 343] = 58996
arr.[ 344] = 59340
arr.[ 345] = 59685
arr.[ 346] = 60031
arr.[ 347] = 60378
arr.[ 348] = 60726
arr.[ 349] = 61075
arr.[ 350] = 61425
arr.[ 351] = 61776
arr.[ 352] = 62128
arr.[ 353] = 62481
arr.[ 354] = 62835
arr.[ 355] = 63190
arr.[ 356] = 63546
arr.[ 357] = 63903
arr.[ 358] = 64261
arr.[ 359] = 64620
arr.[ 360] = 64980
arr.[ 361] = 65341
arr.[ 362] = 65703
arr.[ 363] = 66066
arr.[ 364] = 66430
arr.[ 365] = 66795
arr.[ 366] = 67161
arr.[ 367] = 67528
arr.[ 368] = 67896
arr.[ 369] = 68265
arr.[ 370] = 68635
arr.[ 371] = 69006
arr.[ 372] = 69378
arr.[ 373] = 69751
arr.[ 374] = 70125
arr.[ 375] = 70500
arr.[ 376] = 70876
arr.[ 377] = 71253
arr.[ 378] = 71631
arr.[ 379] = 72010
arr.[ 380] = 72390
arr.[ 381] = 72771
arr.[ 382] = 73153
arr.[ 383] = 73536
arr.[ 384] = 73920
arr.[ 385] = 74305
arr.[ 386] = 74691
arr.[ 387] = 75078
arr.[ 388] = 75466
arr.[ 389] = 75855
arr.[ 390] = 76245
arr.[ 391] = 76636
arr.[ 392] = 77028
arr.[ 393] = 77421
arr.[ 394] = 77815
arr.[ 395] = 78210
arr.[ 396] = 78606
arr.[ 397] = 79003
arr.[ 398] = 79401
arr.[ 399] = 79800
arr.[ 400] = 80200
arr.[ 401] = 80601
arr.[ 402] = 81003
arr.[ 403] = 81406
arr.[ 404] = 81810
arr.[ 405] = 82215
arr.[ 406] = 82621
arr.[ 407] = 83028
arr.[ 408] = 83436
arr.[ 409] = 83845
arr.[ 410] = 84255
arr.[ 411] = 84666
arr.[ 412] = 85078
arr.[ 413] = 85491
arr.[ 414] = 85905
arr.[ 415] = 86320
arr.[ 416] = 86736
arr.[ 417] = 87153
arr.[ 418] = 87571
arr.[ 419] = 87990
arr.[ 420] = 88410
arr.[ 421] = 88831
arr.[ 422] = 89253
arr.[ 423] = 89676
arr.[ 424] = 90100
arr.[ 425] = 90525
arr.[ 426] = 90951
arr.[ 427] = 91378
arr.[ 428] = 91806
arr.[ 429] = 92235
arr.[ 430] = 92665
arr.[ 431] = 93096
arr.[ 432] = 93528
arr.[ 433] = 93961
arr.[ 434] = 94395
arr.[ 435] = 94830
arr.[ 436] = 95266
arr.[ 437] = 95703
arr.[ 438] = 96141
arr.[ 439] = 96580
arr.[ 440] = 97020
arr.[ 441] = 97461
arr.[ 442] = 97903
arr.[ 443] = 98346
arr.[ 444] = 98790
arr.[ 445] = 99235
arr.[ 446] = 99681
arr.[ 447] = 100128
arr.[ 448] = 100576
arr.[ 449] = 101025
arr.[ 450] = 101475
arr.[ 451] = 101926
arr.[ 452] = 102378
arr.[ 453] = 102831
arr.[ 454] = 103285
arr.[ 455] = 103740
arr.[ 456] = 104196
arr.[ 457] = 104653
arr.[ 458] = 105111
arr.[ 459] = 105570
arr.[ 460] = 106030
arr.[ 461] = 106491
arr.[ 462] = 106953
arr.[ 463] = 107416
arr.[ 464] = 107880
arr.[ 465] = 108345
arr.[ 466] = 108811
arr.[ 467] = 109278
arr.[ 468] = 109746
arr.[ 469] = 110215
arr.[ 470] = 110685
arr.[ 471] = 111156
arr.[ 472] = 111628
arr.[ 473] = 112101
arr.[ 474] = 112575
arr.[ 475] = 113050
arr.[ 476] = 113526
arr.[ 477] = 114003
arr.[ 478] = 114481
arr.[ 479] = 114960
arr.[ 480] = 115440
arr.[ 481] = 115921
arr.[ 482] = 116403
arr.[ 483] = 116886
arr.[ 484] = 117370
arr.[ 485] = 117855
arr.[ 486] = 118341
arr.[ 487] = 118828
arr.[ 488] = 119316
arr.[ 489] = 119805
arr.[ 490] = 120295
arr.[ 491] = 120786
arr.[ 492] = 121278
arr.[ 493] = 121771
arr.[ 494] = 122265
arr.[ 495] = 122760
arr.[ 496] = 123256
arr.[ 497] = 123753
arr.[ 498] = 124251
arr.[ 499] = 124750
arr.[ 500] = 125250
arr.[ 501] = 125751
arr.[ 502] = 126253
arr.[ 503] = 126756
arr.[ 504] = 127260
arr.[ 505] = 127765
arr.[ 506] = 128271
arr.[ 507] = 128778
arr.[ 508] = 129286
arr.[ 509] = 129795
arr.[ 510] = 130305
arr.[ 511] = 130816
arr.[ 512] = 131328
arr.[ 513] = 131841
arr.[ 514] = 132355
arr.[ 515] = 132870
arr.[ 516] = 133386
arr.[ 517] = 133903
arr.[ 518] = 134421
arr.[ 519] = 134940
arr.[ 520] = 135460
arr.[ 521] = 135981
arr.[ 522] = 136503
arr.[ 523] = 137026
arr.[ 524] = 137550
arr.[ 525] = 138075
arr.[ 526] = 138601
arr.[ 527] = 139128
arr.[ 528] = 139656
arr.[ 529] = 140185
arr.[ 530] = 140715
arr.[ 531] = 141246
arr.[ 532] = 141778
arr.[ 533] = 142311
arr.[ 534] = 142845
arr.[ 535] = 143380
arr.[ 536] = 143916
arr.[ 537] = 144453
arr.[ 538] = 144991
arr.[ 539] = 145530
arr.[ 540] = 146070
arr.[ 541] = 146611
arr.[ 542] = 147153
arr.[ 543] = 147696
arr.[ 544] = 148240
arr.[ 545] = 148785
arr.[ 546] = 149331
arr.[ 547] = 149878
arr.[ 548] = 150426
arr.[ 549] = 150975
arr.[ 550] = 151525
arr.[ 551] = 152076
arr.[ 552] = 152628
arr.[ 553] = 153181
arr.[ 554] = 153735
arr.[ 555] = 154290
arr.[ 556] = 154846
arr.[ 557] = 155403
arr.[ 558] = 155961
arr.[ 559] = 156520
arr.[ 560] = 157080
arr.[ 561] = 157641
arr.[ 562] = 158203
arr.[ 563] = 158766
arr.[ 564] = 159330
arr.[ 565] = 159895
arr.[ 566] = 160461
arr.[ 567] = 161028
arr.[ 568] = 161596
arr.[ 569] = 162165
arr.[ 570] = 162735
arr.[ 571] = 163306
arr.[ 572] = 163878
arr.[ 573] = 164451
arr.[ 574] = 165025
arr.[ 575] = 165600
arr.[ 576] = 166176
arr.[ 577] = 166753
arr.[ 578] = 167331
arr.[ 579] = 167910
arr.[ 580] = 168490
arr.[ 581] = 169071
arr.[ 582] = 169653
arr.[ 583] = 170236
arr.[ 584] = 170820
arr.[ 585] = 171405
arr.[ 586] = 171991
arr.[ 587] = 172578
arr.[ 588] = 173166
arr.[ 589] = 173755
arr.[ 590] = 174345
arr.[ 591] = 174936
arr.[ 592] = 175528
arr.[ 593] = 176121
arr.[ 594] = 176715
arr.[ 595] = 177310
arr.[ 596] = 177906
arr.[ 597] = 178503
arr.[ 598] = 179101
arr.[ 599] = 179700
arr.[ 600] = 180300
arr.[ 601] = 180901
arr.[ 602] = 181503
arr.[ 603] = 182106
arr.[ 604] = 182710
arr.[ 605] = 183315
arr.[ 606] = 183921
arr.[ 607] = 184528
arr.[ 608] = 185136
arr.[ 609] = 185745
arr.[ 610] = 186355
arr.[ 611] = 186966
arr.[ 612] = 187578
arr.[ 613] = 188191
arr.[ 614] = 188805
arr.[ 615] = 189420
arr.[ 616] = 190036
arr.[ 617] = 190653
arr.[ 618] = 191271
arr.[ 619] = 191890
arr.[ 620] = 192510
arr.[ 621] = 193131
arr.[ 622] = 193753
arr.[ 623] = 194376
arr.[ 624] = 195000
arr.[ 625] = 195625
arr.[ 626] = 196251
arr.[ 627] = 196878
arr.[ 628] = 197506
arr.[ 629] = 198135
arr.[ 630] = 198765
arr.[ 631] = 199396
arr.[ 632] = 200028
arr.[ 633] = 200661
arr.[ 634] = 201295
arr.[ 635] = 201930
arr.[ 636] = 202566
arr.[ 637] = 203203
arr.[ 638] = 203841
arr.[ 639] = 204480
arr.[ 640] = 205120
arr.[ 641] = 205761
arr.[ 642] = 206403
arr.[ 643] = 207046
arr.[ 644] = 207690
arr.[ 645] = 208335
arr.[ 646] = 208981
arr.[ 647] = 209628
arr.[ 648] = 210276
arr.[ 649] = 210925
arr.[ 650] = 211575
arr.[ 651] = 212226
arr.[ 652] = 212878
arr.[ 653] = 213531
arr.[ 654] = 214185
arr.[ 655] = 214840
arr.[ 656] = 215496
arr.[ 657] = 216153
arr.[ 658] = 216811
arr.[ 659] = 217470
arr.[ 660] = 218130
arr.[ 661] = 218791
arr.[ 662] = 219453
arr.[ 663] = 220116
arr.[ 664] = 220780
arr.[ 665] = 221445
arr.[ 666] = 222111
arr.[ 667] = 222778
arr.[ 668] = 223446
arr.[ 669] = 224115
arr.[ 670] = 224785
arr.[ 671] = 225456
arr.[ 672] = 226128
arr.[ 673] = 226801
arr.[ 674] = 227475
arr.[ 675] = 228150
arr.[ 676] = 228826
arr.[ 677] = 229503
arr.[ 678] = 230181
arr.[ 679] = 230860
arr.[ 680] = 231540
arr.[ 681] = 232221
arr.[ 682] = 232903
arr.[ 683] = 233586
arr.[ 684] = 234270
arr.[ 685] = 234955
arr.[ 686] = 235641
arr.[ 687] = 236328
arr.[ 688] = 237016
arr.[ 689] = 237705
arr.[ 690] = 238395
arr.[ 691] = 239086
arr.[ 692] = 239778
arr.[ 693] = 240471
arr.[ 694] = 241165
arr.[ 695] = 241860
arr.[ 696] = 242556
arr.[ 697] = 243253
arr.[ 698] = 243951
arr.[ 699] = 244650
arr.[ 700] = 245350
arr.[ 701] = 246051
arr.[ 702] = 246753
arr.[ 703] = 247456
arr.[ 704] = 248160
arr.[ 705] = 248865
arr.[ 706] = 249571
arr.[ 707] = 250278
arr.[ 708] = 250986
arr.[ 709] = 251695
arr.[ 710] = 252405
arr.[ 711] = 253116
arr.[ 712] = 253828
arr.[ 713] = 254541
arr.[ 714] = 255255
arr.[ 715] = 255970
arr.[ 716] = 256686
arr.[ 717] = 257403
arr.[ 718] = 258121
arr.[ 719] = 258840
arr.[ 720] = 259560
arr.[ 721] = 260281
arr.[ 722] = 261003
arr.[ 723] = 261726
arr.[ 724] = 262450
arr.[ 725] = 263175
arr.[ 726] = 263901
arr.[ 727] = 264628
arr.[ 728] = 265356
arr.[ 729] = 266085
arr.[ 730] = 266815
arr.[ 731] = 267546
arr.[ 732] = 268278
arr.[ 733] = 269011
arr.[ 734] = 269745
arr.[ 735] = 270480
arr.[ 736] = 271216
arr.[ 737] = 271953
arr.[ 738] = 272691
arr.[ 739] = 273430
arr.[ 740] = 274170
arr.[ 741] = 274911
arr.[ 742] = 275653
arr.[ 743] = 276396
arr.[ 744] = 277140
arr.[ 745] = 277885
arr.[ 746] = 278631
arr.[ 747] = 279378
arr.[ 748] = 280126
arr.[ 749] = 280875
arr.[ 750] = 281625
arr.[ 751] = 282376
arr.[ 752] = 283128
arr.[ 753] = 283881
arr.[ 754] = 284635
arr.[ 755] = 285390
arr.[ 756] = 286146
arr.[ 757] = 286903
arr.[ 758] = 287661
arr.[ 759] = 288420
arr.[ 760] = 289180
arr.[ 761] = 289941
arr.[ 762] = 290703
arr.[ 763] = 291466
arr.[ 764] = 292230
arr.[ 765] = 292995
arr.[ 766] = 293761
arr.[ 767] = 294528
arr.[ 768] = 295296
arr.[ 769] = 296065
arr.[ 770] = 296835
arr.[ 771] = 297606
arr.[ 772] = 298378
arr.[ 773] = 299151
arr.[ 774] = 299925
arr.[ 775] = 300700
arr.[ 776] = 301476
arr.[ 777] = 302253
arr.[ 778] = 303031
arr.[ 779] = 303810
arr.[ 780] = 304590
arr.[ 781] = 305371
arr.[ 782] = 306153
arr.[ 783] = 306936
arr.[ 784] = 307720
arr.[ 785] = 308505
arr.[ 786] = 309291
arr.[ 787] = 310078
arr.[ 788] = 310866
arr.[ 789] = 311655
arr.[ 790] = 312445
arr.[ 791] = 313236
arr.[ 792] = 314028
arr.[ 793] = 314821
arr.[ 794] = 315615
arr.[ 795] = 316410
arr.[ 796] = 317206
arr.[ 797] = 318003
arr.[ 798] = 318801
arr.[ 799] = 319600
arr.[ 800] = 320400
arr.[ 801] = 321201
arr.[ 802] = 322003
arr.[ 803] = 322806
arr.[ 804] = 323610
arr.[ 805] = 324415
arr.[ 806] = 325221
arr.[ 807] = 326028
arr.[ 808] = 326836
arr.[ 809] = 327645
arr.[ 810] = 328455
arr.[ 811] = 329266
arr.[ 812] = 330078
arr.[ 813] = 330891
arr.[ 814] = 331705
arr.[ 815] = 332520
arr.[ 816] = 333336
arr.[ 817] = 334153
arr.[ 818] = 334971
arr.[ 819] = 335790
arr.[ 820] = 336610
arr.[ 821] = 337431
arr.[ 822] = 338253
arr.[ 823] = 339076
arr.[ 824] = 339900
arr.[ 825] = 340725
arr.[ 826] = 341551
arr.[ 827] = 342378
arr.[ 828] = 343206
arr.[ 829] = 344035
arr.[ 830] = 344865
arr.[ 831] = 345696
arr.[ 832] = 346528
arr.[ 833] = 347361
arr.[ 834] = 348195
arr.[ 835] = 349030
arr.[ 836] = 349866
arr.[ 837] = 350703
arr.[ 838] = 351541
arr.[ 839] = 352380
arr.[ 840] = 353220
arr.[ 841] = 354061
arr.[ 842] = 354903
arr.[ 843] = 355746
arr.[ 844] = 356590
arr.[ 845] = 357435
arr.[ 846] = 358281
arr.[ 847] = 359128
arr.[ 848] = 359976
arr.[ 849] = 360825
arr.[ 850] = 361675
arr.[ 851] = 362526
arr.[ 852] = 363378
arr.[ 853] = 364231
arr.[ 854] = 365085
arr.[ 855] = 365940
arr.[ 856] = 366796
arr.[ 857] = 367653
arr.[ 858] = 368511
arr.[ 859] = 369370
arr.[ 860] = 370230
arr.[ 861] = 371091
arr.[ 862] = 371953
arr.[ 863] = 372816
arr.[ 864] = 373680
arr.[ 865] = 374545
arr.[ 866] = 375411
arr.[ 867] = 376278
arr.[ 868] = 377146
arr.[ 869] = 378015
arr.[ 870] = 378885
arr.[ 871] = 379756
arr.[ 872] = 380628
arr.[ 873] = 381501
arr.[ 874] = 382375
arr.[ 875] = 383250
arr.[ 876] = 384126
arr.[ 877] = 385003
arr.[ 878] = 385881
arr.[ 879] = 386760
arr.[ 880] = 387640
arr.[ 881] = 388521
arr.[ 882] = 389403
arr.[ 883] = 390286
arr.[ 884] = 391170
arr.[ 885] = 392055
arr.[ 886] = 392941
arr.[ 887] = 393828
arr.[ 888] = 394716
arr.[ 889] = 395605
arr.[ 890] = 396495
arr.[ 891] = 397386
arr.[ 892] = 398278
arr.[ 893] = 399171
arr.[ 894] = 400065
arr.[ 895] = 400960
arr.[ 896] = 401856
arr.[ 897] = 402753
arr.[ 898] = 403651
arr.[ 899] = 404550
arr.[ 900] = 405450
arr.[ 901] = 406351
arr.[ 902] = 407253
arr.[ 903] = 408156
arr.[ 904] = 409060
arr.[ 905] = 409965
arr.[ 906] = 410871
arr.[ 907] = 411778
arr.[ 908] = 412686
arr.[ 909] = 413595
arr.[ 910] = 414505
arr.[ 911] = 415416
arr.[ 912] = 416328
arr.[ 913] = 417241
arr.[ 914] = 418155
arr.[ 915] = 419070
arr.[ 916] = 419986
arr.[ 917] = 420903
arr.[ 918] = 421821
arr.[ 919] = 422740
arr.[ 920] = 423660
arr.[ 921] = 424581
arr.[ 922] = 425503
arr.[ 923] = 426426
arr.[ 924] = 427350
arr.[ 925] = 428275
arr.[ 926] = 429201
arr.[ 927] = 430128
arr.[ 928] = 431056
arr.[ 929] = 431985
arr.[ 930] = 432915
arr.[ 931] = 433846
arr.[ 932] = 434778
arr.[ 933] = 435711
arr.[ 934] = 436645
arr.[ 935] = 437580
arr.[ 936] = 438516
arr.[ 937] = 439453
arr.[ 938] = 440391
arr.[ 939] = 441330
arr.[ 940] = 442270
arr.[ 941] = 443211
arr.[ 942] = 444153
arr.[ 943] = 445096
arr.[ 944] = 446040
arr.[ 945] = 446985
arr.[ 946] = 447931
arr.[ 947] = 448878
arr.[ 948] = 449826
arr.[ 949] = 450775
arr.[ 950] = 451725
arr.[ 951] = 452676
arr.[ 952] = 453628
arr.[ 953] = 454581
arr.[ 954] = 455535
arr.[ 955] = 456490
arr.[ 956] = 457446
arr.[ 957] = 458403
arr.[ 958] = 459361
arr.[ 959] = 460320
arr.[ 960] = 461280
arr.[ 961] = 462241
arr.[ 962] = 463203
arr.[ 963] = 464166
arr.[ 964] = 465130
arr.[ 965] = 466095
arr.[ 966] = 467061
arr.[ 967] = 468028
arr.[ 968] = 468996
arr.[ 969] = 469965
arr.[ 970] = 470935
arr.[ 971] = 471906
arr.[ 972] = 472878
arr.[ 973] = 473851
arr.[ 974] = 474825
arr.[ 975] = 475800
arr.[ 976] = 476776
arr.[ 977] = 477753
arr.[ 978] = 478731
arr.[ 979] = 479710
arr.[ 980] = 480690
arr.[ 981] = 481671
arr.[ 982] = 482653
arr.[ 983] = 483636
arr.[ 984] = 484620
arr.[ 985] = 485605
arr.[ 986] = 486591
arr.[ 987] = 487578
arr.[ 988] = 488566
arr.[ 989] = 489555
arr.[ 990] = 490545
arr.[ 991] = 491536
arr.[ 992] = 492528
arr.[ 993] = 493521
arr.[ 994] = 494515
arr.[ 995] = 495510
arr.[ 996] = 496506
arr.[ 997] = 497503
arr.[ 998] = 498501
arr.[ 999] = 499500

Using arrays

Category:
Arrays, Hash Tables and Dictionaries
Description: Create a histogram of the occurrences of particular letters
Code:
let SampleArray2() =
let numLetters = 26
let results = Array.create numLetters 0
let data = "The quick brown fox jumps over the lazy dog"
for i = 0 to data.Length - 1 do
let c = data.Chars(i)
let c = Char.ToUpper(c)
if c >= 'A' && c <= 'Z' then
let i = int c - int 'A'
results.[i] <- results.[i] + 1
for i = 0 to numLetters - 1 do
printfn "Number of '%c' characters = %d" (char (i + int 'A')) results.[i]

Execution Result:
Number of 'A' characters = 1
Number of 'B' characters = 1
Number of 'C' characters = 1
Number of 'D' characters = 1
Number of 'E' characters = 3
Number of 'F' characters = 1
Number of 'G' characters = 1
Number of 'H' characters = 2
Number of 'I' characters = 1
Number of 'J' characters = 1
Number of 'K' characters = 1
Number of 'L' characters = 1
Number of 'M' characters = 1
Number of 'N' characters = 1
Number of 'O' characters = 4
Number of 'P' characters = 1
Number of 'Q' characters = 1
Number of 'R' characters = 2
Number of 'S' characters = 1
Number of 'T' characters = 2
Number of 'U' characters = 2
Number of 'V' characters = 1
Number of 'W' characters = 1
Number of 'X' characters = 1
Number of 'Y' characters = 1
Number of 'Z' characters = 1

Using System.Collections.Generic.Dictionary

Category:
Arrays, Hash Tables and Dictionaries
Description: Create a histogram of the occurrences of particular unicode characters using a dictionary.
Code:
let SampleHashtbl3() =
let tab = new System.Collections.Generic.Dictionary(30)
let data = "The quick brown fox jumps over the lazy dog"
for i = 0 to data.Length - 1 do
let c = data.Chars(i)
if tab.ContainsKey(c) then
let v = tab.[c]
let _ = tab.Remove(c)
tab.Add(c,v+1)
else
tab.Add(c,1)

for KeyValue(k,v) in tab do
printfn "Number of '%c' characters = %d" k v

//--------------------------------------------------------

Execution Result:
Number of 'T' characters = 1
Number of 'h' characters = 2
Number of 'e' characters = 3
Number of ' ' characters = 8
Number of 'q' characters = 1
Number of 'u' characters = 2
Number of 'i' characters = 1
Number of 'c' characters = 1
Number of 'k' characters = 1
Number of 'b' characters = 1
Number of 'r' characters = 2
Number of 'o' characters = 4
Number of 'w' characters = 1
Number of 'n' characters = 1
Number of 'f' characters = 1
Number of 'x' characters = 1
Number of 'j' characters = 1
Number of 'm' characters = 1
Number of 'p' characters = 1
Number of 's' characters = 1
Number of 'v' characters = 1
Number of 't' characters = 1
Number of 'l' characters = 1
Number of 'a' characters = 1
Number of 'z' characters = 1
Number of 'y' characters = 1
Number of 'd' characters = 1
Number of 'g' characters = 1

Last edited Sep 12, 2011 at 11:13 PM by ttliu2000, version 1

Comments

No comments yet.