F# List.Map using a random number - f#

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.

Related

Go through a list until some condition is met, in F#

let's take this data:
let a = [10; 11; 12; 13; 14; 0; 15; 16]
I'm trying to do this:
[
let mutable skip = false
for i in 0 .. a.Length - 1 do
if a.[i] = 0 then skip <- true
if not skip then yield a.[i]
]
but I was wondering if List.unfold could be used for this? (and how?)
In practice, I'm getting a sequence of sequences (seq of rows, each holding a seq of columns, from an Excel file), and I want to stop the parsing when I encounter an empty line, but the simplified example illustrates it.
The expression above works, so this is about me learning if unfold could be applied to this.
I would use takeWhile:
let a = [10; 11; 12; 13; 14; 0; 15; 16]
Seq.takeWhile ((<>) 0) a
// |> do your parsing
Yes, you can use List.unfold:
let a = [10; 11; 12; 13; 14; 0; 15; 16]
a
|> List.unfold (function
| [] -> None
| x :: rest -> if x = 0 then None else Some (x, rest)
)

Easy way to convert int [,] to int [] [] in F# [duplicate]

This question already has answers here:
F# converting Array2D to array of arrays
(2 answers)
Closed 5 years ago.
We have two arrays
let a = [| [|1|] |]
and
let a' = Array2D.init 1 1 (fun x y -> 1)
the first returns int [][] and the second int [,]
Is there an easy way to convert int[,] to int [][] ?
There are no built-in functions to do this. I would use list comprehension syntax, that would seem straightforward:
let a =
[| for i in 0..(Array2D.length1 a' - 1) ->
[| for j in 0..(Array2D.length2 a' - 1) -> a'.[i,j] |] |]
One thing to look out for is the base index. In your example the array is zero-based, so I used zeroes just to make the code shorter. But if you need to support the case of non-zero-based arrays, you have to do a'.[ i + Array2D.base1 a', j + Array2D.base2 a' ] instead. Not quite as elegant, but watchagonnado.
If it's the building of the array you're interested in, you can build the [][] (aka jagged array) form by using two Array.init
e.g.
Array.init 10 (fun r -> Array.init 20 (fun c -> (c+r)))
[|[|0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19|];
[|1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20|];
[|2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21|];...

Remove a single element from an array

I've been at this for a few hours now, looking though every website and piece of documentation I could. I can't figure out how to remove one, and only one element (In this case, a string) from an array, keeping any duplicates in tact.
I did find a way, but, it's absolutely atrocious:
let remItem gs item =
if (chkItem gs item) then
let mutable fr = [| |] //temporary array
let mutable don = false //check if we found the element
for i in gs.inventory do
if not (i = item) && don then
fr <- (Array.append fr [|i|])
//add to the temp array until we find our item
elif i = item && don = false then don <- true
//we found it, skip just once so it doesn't get added
elif don then fr <- (Array.append fr [|i|])
//now just add everything else to the temp array
{ gs with inventory = fr }
else gs
I wrote this and I barely know how it works. Please tell me there's a better way to do this. I know the mutable variables aren't needed, but I've written a dozen equally horrendous-looking pure functions and concluded this is the best that I could do. I've tried a lot of the Array.* recursive functions already, I can't seem to make any of those comply with what I want either. I just want to know if it's even possible to do this neatly and purely in F#.
I think the easiest way to do this is to first look for the index (it's an array after all) and then just cut this out - this is (I think) a good compromise between performance and pureness - it's a pure operation but you don't get to much copy-operations:
let remove x (xs : 'a array) =
match Array.tryFindIndex ((=) x) xs with
| Some 0 -> xs.[1..]
| Some i -> Array.append xs.[..i-1] xs.[i+1..]
| None -> xs
please note that you have to take care of it beeing the first index because xs.[..(-1)] will throw an exception (while the other edge-case is ok):
> remove 0 [|1..10|];;
val it : int [] = [|1; 2; 3; 4; 5; 6; 7; 8; 9; 10|]
> remove 1 [|1..10|];;
val it : int [] = [|2; 3; 4; 5; 6; 7; 8; 9; 10|]
> remove 3 [|1..10|];;
val it : int [] = [|1; 2; 4; 5; 6; 7; 8; 9; 10|]
> remove 9 [|1..10|];;
val it : int [] = [|1; 2; 3; 4; 5; 6; 7; 8; 10|]
> remove 10 [|1..10|];;
val it : int [] = [|1; 2; 3; 4; 5; 6; 7; 8; 9|]
> remove 11 [|1..10|];;
val it : int [] = [|1; 2; 3; 4; 5; 6; 7; 8; 9; 10|]
if you need even more performance you could create an empty array and use a more imperative style to copy the parts:
let remove x (xs : 'a array) =
match Array.tryFindIndex ((=) x) xs with
| Some i ->
let res = Array.zeroCreate (xs.Length-1)
if i >= 1 then
System.Array.Copy(xs,0,res,0,i)
if i+1 < xs.Length then
System.Array.Copy(xs,i+1,res,i,xs.Length-i-1)
res
| None -> xs
Remove the first occurrence of a item from a list (taken from http://www.fssnip.net/1T):
let rec remove_first pred lst =
match lst with
| h::t when pred h -> t
| h::t -> h::remove_first pred t
| _ -> []
Usage:
let somelist = [('a',2);('f',7);('a',4);('h',10)]
let removed = somelist |> remove_first (fun (x,y) -> x='a')
// Result is:
// [('f',7);('a',4);('h',10)]
Fold should do the trick:
let remove x a =
Array.fold
(fun (s,found) t ->
if found || t <> x then Array.append s [|t|],found
else s,true) ([||],false) a |> fst
Example usage:
remove 2 [|1; 2; 3; 4; 2; 5|]
val it : int [] = [|1; 3; 4; 2; 5|]

F# finding only prime numbers

I'm recently new to F# so please bear with me. The problem i have is I'm trying to find only prime numbers.
I've write this code:
let isPrime n =
let rec check i =
i > n/2 || (n % i <> 0 && check (i + 1))
check 2;;
let listNums = List.filter isPrime >> List.length;;
let nums = [ 16; 17; 3; 4; 2; 5; 11; 6; 7; 18; 13; 14; ];;
let countPrimes (x:int) = x |> List.ofSeq |> listNums;;
trying to call
countPrimes nums;;
but this is failed with message:
The type 'int' is not compatible with the type 'seq<'a>'
any help would be appreciated
I found the solution
let isPrime n =
let rec check i =
i > n/2 || (n % i <> 0 && check (i + 1))
check 2;;
let listNums = List.filter isPrime >> List.length;;
let nums = [| 16; 17; 3; 4; 2; 5; 11; 6; 7; 18; 13; 14; |];;
let countPrimes (x:int[]) = x |> List.ofSeq |> listNums;;
countPrimes nums;;
Thanks all!
x |> List.ofSeq
seems to be the problem to me. You are passing an int into a function that requires a list. List.toSeq changes a list into a sequence. You want the function countPrimes to take a List of integers, not simply an integer. Although Carsten is right, listNums already takes a List of integers (edit: and computes the value you want provided isPrime is correct).
You do not need to countPrimes separately. Enough to remove and will work:
let isPrime n =
let rec check i =
i > n/2 || (n % i <> 0 && check (i + 1))
check 2
let nums = [ 16; 17; 3; 4; 2; 5; 11; 6; 7; 18; 13; 14; ]
let listPrime lst =
lst |> List.filter isPrime
nums |> listPrime |> printfn "%A"
Out:
[17; 3; 2; 5; 11; 7; 13]
Link:
https://dotnetfiddle.net/nVXwZ5

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]

Resources