How to generate a multiplication table sequence? - f#

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

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]]

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.

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]]

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

Related

Is there a way in f# to perform a cross operation on lists?

Is there a way in f# to perform an opreation on the all the possible element combinations of two lists in f#?
Example
l1 = [1;2;3]
l2=[4;5;6]
let plus x y = x+y
Then fun plus l1 l2 would perform [(1+4);(1+5);(1+6);(2+4);(2+5);(2+6);(3+4);(3+5);(3+6)]
Hence the output: [5;6;7;6;7;8;7;8;9]
Note: I have tried using zip but it only takes each element once.
Yep, easiest way is to use a list comprehension.
let t1 = [1;2;3]
let t2 = [4;5;6]
[for a in t1 do for b in t2 do yield a+b] //val it : int list = [5; 6; 7; 6; 7; 8; 7; 8; 9]
//as a function
let f lst1 lst2 = [for a in lst1 do for b in lst2 do yield a+b]
Another possibility is to combine a List.collect with a List.map:
let l1 = [1;2;3]
let l2 = [4;5;6]
l1 |> List.collect (fun x -> List.map ((+) x) l2) //output: [5; 6; 7; 6; 7; 8; 7; 8; 9]

Why does foldBack not execute the same side-effects that fold does?

I was working through the answers to Example of the difference between List.fold and List.foldBack trying to get my head around the difference between fold and foldBack. I understand the difference in application order now, but there's a difference in side-effects that I don't understand.
I used List.fold and List.foldBack for my testing. My accumulator functions that were basically equivalent to ::, so that accumulation order would matter. The accumulator functions I used were as follows:
let f acc x =
// printfn "Folding %A into %A" x acc // Side-effect!
x :: acc
let f2 x acc =
// printfn "Folding %A into %A" x acc // Side-effect!
x :: acc
I understand from the F# reference that:
List.fold f [] [1; 2; 3; 4; 5] = (f (f (f (f (f [] 1) 2) 3) 4) 5)
and:
List.foldBack f2 [] [1; 2; 3; 4; 5] = (f2 1 (f2 2 (f2 3 (f2 4 (f2 5 [])))))
should both return true, and they do. Great, I thought; I understand how it works. But just to make sure, I uncommented the side-effect lines from f and f2 and ran List.fold f and List.foldBack f2 again. Result of List.fold f [] [1; 2; 3; 4; 5] with the printfn line uncommented:
Folding 1 into []
Folding 2 into [1]
Folding 3 into [2; 1]
Folding 4 into [3; 2; 1]
Folding 5 into [4; 3; 2; 1]
val it : bool = true
Result of List.foldBack f2 [] [1; 2; 3; 4; 5] with the printfn line uncommented:
val it : bool = true
I was expecting "Folding N into [list]" to show up in both cases. But List.fold executed the side effects of its accumulator function, and List.foldBack did not.
Why is there a difference in side-effect execution between the two forms of fold?
You have the arguments in the wrong order.
It should be
> List.foldBack f2 [1; 2; 3; 4; 5] [];;
Folding 5 into []
Folding 4 into [5]
Folding 3 into [4; 5]
Folding 2 into [3; 4; 5]
Folding 1 into [2; 3; 4; 5]
val it : int list = [1; 2; 3; 4; 5]

How do you extract distinct elements from a list?

I'm pretty new to f# and I'm having a hard time trying to extract a list of distinct values from a list:
let myList = [ 1; 2; 2; 3; 4; 3 ]
// desired list
[ 1; 2; 3; 4 ]
How do I do that? I see that seq has a distinct method, but not lists.
let myList = [ 1; 2; 2; 3; 4; 3 ]
let distinctList = myList |> Seq.distinct |> List.ofSeq
Result:
>
val myList : int list = [1; 2; 2; 3; 4; 3]
val distinctList : int list = [1; 2; 3; 4]
Next F# version (4.0) will have List.distinct function
Not sure if worse or better than Petr answer but you could also do :
let distinct xs = xs |> Set.ofList |> Set.toList
> distinct [ 1; 2; 2; 3; 4; 3 ];;
val it : int list = [1; 2; 3; 4]

Slicing in a 2D Array in F# but similar to Matlab?

I was wondering if there is a way to use a list or an array of int as an index for slicing into a array to get a sub array in f#.
I know you can do the following
Arr2d.[*,1] or Arr2d.[1..5,1..2] etc...
But I was looking for something like in Matlab where you can write:
Arr2d([1;6;10],1) or Arr2d(1:10,[1;6;10])
Is it possible to do slicing like that in F#?
Thanks!
Here was my sample solution: (maybe not optimal but works)
let sampleMatrix = Array2D.init 10 5 (fun x y -> y)
val sampleMatrix : int [,] = [[0; 1; 2; 3; 4]
[0; 1; 2; 3; 4]
[0; 1; 2; 3; 4]
[0; 1; 2; 3; 4]
[0; 1; 2; 3; 4]
[0; 1; 2; 3; 4]
[0; 1; 2; 3; 4]
[0; 1; 2; 3; 4]
[0; 1; 2; 3; 4]
[0; 1; 2; 3; 4]]
let idxlist = [1;3;4]
let array2dColumnSlice (idxlist:list<int>) (data:'T[,]) =
let tmp = [|for i in idxlist -> data.[*,i]|]
Array2D.init tmp.[0].Length tmp.Length (fun x y -> tmp.[y].[x] )
let slice = array2dColumnSlice idxlist sampleMatrix
val slice : int [,] = [[1; 3; 4]
[1; 3; 4]
[1; 3; 4]
[1; 3; 4]
[1; 3; 4]
[1; 3; 4]
[1; 3; 4]
[1; 3; 4]
[1; 3; 4]
[1; 3; 4]]
As detailed here, there is no additional slicing notation beyond what you have already found.
For ranges only, it is possible, by wrapping Array2D with a sliceable type, or using the old PowerPack's Matrix type.
See the docs here:
http://msdn.microsoft.com/en-us/library/dd233214.aspx#sectionToggle6
You can use this slicing syntax for types that implement the element
access operators and overloaded GetSlice methods. For example, the
following code creates a Matrix type that wraps the F# 2D array,
implements an Item property to provide support for array indexing, and
implements three versions of GetSlice. If you can use this code as a
template for your matrix types, you can use all the slicing operations
that this section describes.

F# List.Map using a random number

I want to create a list of 20 random numbers. I wrote this:
let numberList = [ 1 .. 20 ]
let randoms =
numberList
|> List.map (fun (x) -> System.Random().Next(0,9))
And I got this:
val numberList : int list =
[1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20]
val randoms : int list =
[7; 7; 7; 7; 7; 7; 7; 7; 7; 7; 7; 7; 7; 7; 7; 7; 7; 7; 7; 7]
Which makes sense. The problem is that I want to pass in a random number each time the function is evaluated like this:
let numberList = [ 1 .. 20 ]
let randoms =
numberList
|> List.map (fun (Random().Next(0,9)) -> x)
but I am getting a "The pattern discriminator 'Random' is not defined" exception.
Am i approaching this problem the wrong way?
Thanks in advance
In your original version, you create a new Random object at each iteration. As this is seeded with the current time, you always create the same sequence.
You need to create the object outside map like so
let RNG = new System.Random()
numberlist |> List.map (fun x -> RNG.Next(0,9))
Although a more elegant solution would be
let RNG = new System.Random()
let randoms = List.init 20 (fun _ -> RNG.Next(0,9))
Your second version fails because you are trying to treat Random as a pattern which makes no sense in this situation.

Resources