locked
Tutorial List questions - RRS feed

  • Question

  • let firstHundred = [0..100]  note .. works ... does not

    end of resulting list

    99; ...]

     0 is listed but 100 is not listed -Why? - what is the significance of the ... ?

    Run

    let firstHundred = [0..101]

    end of resulting list

    99; ...]  ??

    let firstHundred = [0..100]
    List.map (fun x -> x * 2)
        (List.filter (fun x -> x % 2 = 0) firstHundred)

    val

    196; 200]

    Where did the 200 come from if 100 is not listed??


    rrc

    Tuesday, January 29, 2013 2:53 AM

Answers

  • This is to avoid taking too much too much places in FSI Window. Imagine you create a list : 

    let firstFiveHundred = [0 .. 500];;

    If each member were displayed in FSI, the place occupied by window will be too large but there really 501 members.

    • Marked as answer by Brahman Wednesday, January 30, 2013 4:42 PM
    Wednesday, January 30, 2013 3:23 PM
  • The numbers of the characters(integers) in the list is not actually 100 but 101. The output is showing upto 99 in the sequence. Even if you type let firstThreeHundred = [0..300] it will show the sequence till 99 only. This is the behaviour of the F# Interactive in the Visual studio too.

    When you are defining a list is this way, let firstThreeHundred = [0..300];; you are creating a list with the lower and upper bound included.

    • Proposed as answer by Plepilov Wednesday, January 30, 2013 3:01 PM
    • Marked as answer by Brahman Wednesday, January 30, 2013 4:42 PM
    Wednesday, January 30, 2013 8:53 AM

All replies

  • Hi,

    I do not know the significance of the "triple dot" (may be truncation) but the F# Interactive works that way. Even in the Visual studio 2010 this is the behaviour of F# Interactive.

    In the first defination 

     let firstHundred = [0..100]

    the count or length of the list is 101 with 0 and 100 inclusive. 

    In the last defination

     let firstHundred = [0..101]

    the length of the list is 102 with 0 and 101 inclusive.

    So, 100 is in the list. See the code run below.

    let firstHundred = [0..100] 
    let count = firstHundred.Length
    let maphundred = List.map (fun x -> x * 2) firstHundred
    let count1 = maphundred.Length
    let filterhundred = List.filter (fun x -> x % 2 = 0) firstHundred
    let count2 = filterhundred.Length
    let mapahundred = List.map (fun x -> x * 2) firstHundred
    let count3 = mapahundred.Length
    let firstaHundred = [0..101]
    let count4 = firstaHundred.Length

    > let firstHundred = [0..100] 
    
    val firstHundred : int list =
      [0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20;
       21; 22; 23; 24; 25; 26; 27; 28; 29; 30; 31; 32; 33; 34; 35; 36; 37; 38; 39;
       40; 41; 42; 43; 44; 45; 46; 47; 48; 49; 50; 51; 52; 53; 54; 55; 56; 57; 58;
       59; 60; 61; 62; 63; 64; 65; 66; 67; 68; 69; 70; 71; 72; 73; 74; 75; 76; 77;
       78; 79; 80; 81; 82; 83; 84; 85; 86; 87; 88; 89; 90; 91; 92; 93; 94; 95; 96;
       97; 98; 99; ...]
    
    > let count = firstHundred.Length
    
    val count : int = 101
    
    > let maphundred = List.map (fun x -> x * 2) firstHundred
    
    val maphundred : int list =
      [0; 2; 4; 6; 8; 10; 12; 14; 16; 18; 20; 22; 24; 26; 28; 30; 32; 34; 36; 38;
       40; 42; 44; 46; 48; 50; 52; 54; 56; 58; 60; 62; 64; 66; 68; 70; 72; 74; 76;
       78; 80; 82; 84; 86; 88; 90; 92; 94; 96; 98; 100; 102; 104; 106; 108; 110;
       112; 114; 116; 118; 120; 122; 124; 126; 128; 130; 132; 134; 136; 138; 140;
       142; 144; 146; 148; 150; 152; 154; 156; 158; 160; 162; 164; 166; 168; 170;
       172; 174; 176; 178; 180; 182; 184; 186; 188; 190; 192; 194; 196; 198; ...]
    
    > let count1 = maphundred.Length
    
    val count1 : int = 101
    
    > let filterhundred = List.filter (fun x -> x % 2 = 0) firstHundred
    
    val filterhundred : int list =
      [0; 2; 4; 6; 8; 10; 12; 14; 16; 18; 20; 22; 24; 26; 28; 30; 32; 34; 36; 38;
       40; 42; 44; 46; 48; 50; 52; 54; 56; 58; 60; 62; 64; 66; 68; 70; 72; 74; 76;
       78; 80; 82; 84; 86; 88; 90; 92; 94; 96; 98; 100]
    
    > let count2 = filterhundred.Length
    
    val count2 : int = 51
    
    > let mapahundred = List.map (fun x -> x * 2) firstHundred
    
    val mapahundred : int list =
      [0; 2; 4; 6; 8; 10; 12; 14; 16; 18; 20; 22; 24; 26; 28; 30; 32; 34; 36; 38;
       40; 42; 44; 46; 48; 50; 52; 54; 56; 58; 60; 62; 64; 66; 68; 70; 72; 74; 76;
       78; 80; 82; 84; 86; 88; 90; 92; 94; 96; 98; 100; 102; 104; 106; 108; 110;
       112; 114; 116; 118; 120; 122; 124; 126; 128; 130; 132; 134; 136; 138; 140;
       142; 144; 146; 148; 150; 152; 154; 156; 158; 160; 162; 164; 166; 168; 170;
       172; 174; 176; 178; 180; 182; 184; 186; 188; 190; 192; 194; 196; 198; ...]
    
    > let count3 = mapahundred.Length
    
    val count3 : int = 101
    
    > let firstaHundred = [0..101]
    
    val firstaHundred : int list =
      [0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20;
       21; 22; 23; 24; 25; 26; 27; 28; 29; 30; 31; 32; 33; 34; 35; 36; 37; 38; 39;
       40; 41; 42; 43; 44; 45; 46; 47; 48; 49; 50; 51; 52; 53; 54; 55; 56; 57; 58;
       59; 60; 61; 62; 63; 64; 65; 66; 67; 68; 69; 70; 71; 72; 73; 74; 75; 76; 77;
       78; 79; 80; 81; 82; 83; 84; 85; 86; 87; 88; 89; 90; 91; 92; 93; 94; 95; 96;
       97; 98; 99; ...]
    
    > let count4 = firstaHundred.Length
    
    val count4 : int = 102
    
    > 


     
    • Proposed as answer by Kalnemi Tuesday, January 29, 2013 6:37 AM
    Tuesday, January 29, 2013 6:37 AM
  • If the number of characters (intergers) in the list is actually (as printed in the output) 100

    Why does the count give 101 characters?

    Why are the output lists for [0..100] and [0..101] identical? - but the count values are different?


    rrc

    Tuesday, January 29, 2013 2:57 PM
  • The numbers of the characters(integers) in the list is not actually 100 but 101. The output is showing upto 99 in the sequence. Even if you type let firstThreeHundred = [0..300] it will show the sequence till 99 only. This is the behaviour of the F# Interactive in the Visual studio too.

    When you are defining a list is this way, let firstThreeHundred = [0..300];; you are creating a list with the lower and upper bound included.

    • Proposed as answer by Plepilov Wednesday, January 30, 2013 3:01 PM
    • Marked as answer by Brahman Wednesday, January 30, 2013 4:42 PM
    Wednesday, January 30, 2013 8:53 AM
  • This is to avoid taking too much too much places in FSI Window. Imagine you create a list : 

    let firstFiveHundred = [0 .. 500];;

    If each member were displayed in FSI, the place occupied by window will be too large but there really 501 members.

    • Marked as answer by Brahman Wednesday, January 30, 2013 4:42 PM
    Wednesday, January 30, 2013 3:23 PM