tags:

views:

534

answers:

4

I want to generate a sequence like a multiplication table. So for a start of 1 and a stop of 10 I am looking for a sequence like

1, 2, 3, 4, 5, 6, 7, 8, 9, 10,  // 1*1 - 1*10
2, 4, 6, 8, 10, 12, 14, 16, 18, 20, // 2*1 - 2*10
3, 6, 9, 12, ... // 3*1 - 3*10

Here is my lame start at it, however I can't seem to figure out how to cleanly increment j when the stop is reached, or how to reset i back to the start.

let multable (start,stop) =
    (start,start)
    |> Seq.unfold(
        fun (i,j) ->
        Some(i*j, (i+1, j)))

let its = multable(1, 1)

let first10 = Seq.take 10 its
printf "%A" (Seq.to_list first10)

Which of course gives me 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

+3  A: 

Use a sequence expression:

let multable start stop = seq{
    for a = start to stop do
      for b = start to stop do
        yield a*b
}

Output:

> multable 1 3 |> Seq.to_list;;
val it : int list = [1; 2; 3; 2; 4; 6; 3; 6; 9]

It's weird to represent a fundamentally 2d structure in this fashion. Why not a sequence of sequences:

let multable2 start stop = seq {
    for a = start to stop do yield seq {
      for b = start to stop do
        yield a*b
    }
}

Output:

val multable2 : int -> int -> seq<seq<int>>

> multable2 1 3 |> Seq.to_list;;
val it : seq<int> list = [seq [1; 2; 3]; seq [2; 4; 6]; seq [3; 6; 9]]

If you want to be "clever" and avoid multiplication:

let multable4 start stop = seq {
    for a = start to stop do yield seq {
      let s = ref 0 in
      for b = start to stop do
        s:=!s+a
        yield !s
    }
}

I don't actually see any nice prepackaged "sequence from a to b" outside of comprehensions/sequence expressions, although there's obviously [a..b] (list) and [|a..b|] (array) which you can project through Seq.unfold, Seq.map, etc. to make a Seq.

wrang-wrang
nice
Byron Whitlock
Thanks, I ended up going with the first construct. I want to be able to filter the Sequence of all of these numbers. It might be possible to filter the 2nd 'dimension' of those sequences, however this seems easier to me.
esac
+3  A: 

I can't really think of many cases where I'd prefer Seq.unfold over an equivalent list comprehension:

> let multiplication n m = [for a in 1 .. n -> [for b in 1 .. m -> a * b ] ];;

val multiplication : int -> int -> int list list

> multiplication 5 5;;
val it : int list list =
  [[1; 2; 3; 4; 5]; [2; 4; 6; 8; 10]; [3; 6; 9; 12; 15]; [4; 8; 12; 16; 20];
   [5; 10; 15; 20; 25]]

Occasionally the Array.init methods are useful too:

> let multiplication n m = Array2D.init n m (fun n m -> n * m);;

val multiplication : int -> int -> int [,]

> multiplication 5 5;;
val it : int [,] = [[0; 0; 0; 0; 0]
                    [0; 1; 2; 3; 4]
                    [0; 2; 4; 6; 8]
                    [0; 3; 6; 9; 12]
                    [0; 4; 8; 12; 16]]
Juliet
+1  A: 
let Table r c =
    [for row in 1..r do
        yield [for col in 1..c do
                    yield row * col]]
printfn "%A" (Table 5 4)                    
// [[1; 2; 3; 4]; [2; 4; 6; 8]; [3; 6; 9; 12]; [4; 8; 12; 16]; [5; 10; 15; 20]]
Brian
A: 

Here's another way to use sequences:

let rec fromXToY x y =
  seq {
    if x <= y
    then yield x; yield! fromXToY (x + 1) y;
    else ()
  }

let scaledSequence factor items =
  Seq.map (fun x -> factor * x) items

let mulTable x y =
  let sequenceOfIntegersMultipliedByValue = (fun n -> scaledSequence n (fromXToY x y))
  let seqOfSeqOfValues = Seq.map sequenceOfIntegersMultipliedByValue (fromXToY x y)
  // Convert the sequence of sequences to a simple sequence of values
  Seq.fold Seq.append Seq.empty seqOfSeqOfValues
James Moore