F#: Array of n Empty Lists - arrays

I want to create an Array which contains n empty lists, but the compiler has some problems with the type.
Both
Array.create 5 []
and
[| for i in 1..5 -> [] |]
does not work. Does anyone have any ideas what´s wrong with my code?
I expect the following output:
[| []; []; []; []; [] |]
The reason I want to create such an Array is that I in the next step want to create lists step by step in the Array by appending elements to the empty lists.
Thx in advance

You need to add type annotations or F# cannot say what type of list [] has.
For example, create an array of int list like so:
> Array.create<int list> 5 [] ;;
val it : int list [] = [|[]; []; []; []; []|]
Or in a generic function:
> let foo<'t> n = Array.create<(int * 't) list> n ;;
val foo : n:int -> ((int * 't) list -> (int * 't) list [])

Related

f# Finding the difference between 2 obj[]lists

I have 2 obj[]lists list1 and list2. List1 has a length of 8 and list2 has a length of 10. There are arrays in list1 that only exist in list1. That also goes the same for list2. But there are array that exist in both. I'm wondering how to get the arrays that exist in list1. At the moment when I run my code I get a list of the arrays that exist in both lists, but it's missing the data unique to list1. I'm wondering how to get that unique list1 data. Any suggestions?
let getProdOnly (index:int)(list1:obj[]list)(list2:obj[]list) =
let mutable list3 = list.Empty
for i = 0 to list1.Length-1 do
for j = 0 to list2.Length-1 do
if list1.Item(i).GetValue(index).Equals(list2.Item(j).GetValue(index)) then
System.Diagnostics.Debug.WriteLine("Exists in List 1 and 2")
else
list3 <- list1.Item(i)
Something like this:
let ar1 = [|1;2;3|]
let ar2 = [|2;3;4|]
let s1 = ar1 |> Set.ofArray
let s2 = ar2 |> Set.ofArray
Set.difference s1 s2
//val it : Set<int> = set [1]
There are also a bunch of Array related functions, like compareWith, distinct, exists if you want to work with Arrays directly.
But as was pointed out in previous answers, this type of imperative code is not very idiomatic. Try to avoid mutable variables, try to avoid loops. It could probably rewritten with Array.map for example.
If you want the elements unique to one list, this is the easiest way to do it in F# 4.0:
list1
|> List.except list2
which will remove all the elements of list2 from list1. Note that except also calls a distinct, so you might need to watch out for that.
First I took your code with minor changes and added some printf debuging to see what is does.
let getProdOnly2 (index:int)(list1:obj[] list)(list2:obj[] list) =
let mutable list3 : obj[] list= list.Empty
for i = 0 to list1.Length-1 do
for j = 0 to list2.Length-1 do
if list1.[i].[index] = list2.[j].[index] then
printfn "equal"
System.Diagnostics.Debug.WriteLine("Exists in List 1 and 2")
list3
else
printfn "add %A %A" (list1.Item(i)) (list2.Item(j))
list3 <- list1.Item(i) :: list3
list3
list3
And it does adding an element each time it finds an element not equal the current element.
So my attempt is to take the list1 and just ceep or better filter the elements that are not part of list2.
let getProdOnly3 (index:int)(list1:obj[] list)(list2:obj[] list) =
list1
|> List.filter (fun el1 ->
list2
|> List.fold (fun acc el2 -> acc && (el2<>el1)) true )
I tested the code with the following lists
let list1 = [ [| 1;2;3;4|]
[| 1;2;3;4|]
[| 2;3;4|]
[| 3;4;5|] ] |> List.map (fun a -> a |> Array.map (fun e -> box e))
let list2 = [ [| 2;3;4|]
[| 3;4;5|] ] |> List.map (fun a -> a |> Array.map (fun e -> box e))
In difference to s952163 my result will have double entries if list1 has double entries, do not know if that is wanted or unwanted beahyuvier.

How to get the first index of a given element in Array/List in Elm?

In Javascript, one could use indexOf to get the first instance of a given element in an array.
What's the best way to do this in Elm?
array = Array.fromList ["Stack","Overflow","is","","awesome"]
element = ""
indexOf element array =
-- should return 3
What if we have a list instead of an array?
There is a third party module elm-list-extra that provides this function. "elemIndex" (works for 0.19).
And this one for < 0.19 elemIndex
Just for fun, here's a hand-rolled version for List:
helper : List a -> a -> Int -> Int
helper lst elem offset =
case lst of
[] -> -1
x :: xs ->
if x == elem then offset
else helper xs elem (offset + 1)
indexOf lst element =
helper lst element 0
helper recursively searches the list for the element, keeping track of the number of elements we've visited so far. For the empty list, it returns -1.
If you don't like magic constants, you could easily change the return type to Maybe Int and return Nothing instead.
And here's a complete program using it (just copy + paste into the online REPL at elm-lang.org:
import Html exposing (text)
lst = ["Stack","Overflow","is","","awesome"]
element = ""
helper : List a -> a -> Int -> Int
helper lst elem offset =
case lst of
[] -> -1
x :: xs ->
if x == elem then offset
else helper xs elem (offset + 1)
indexOf lst element =
helper lst element 0
main =
text (toString (indexOf lst ""))
Another take, composing core library functions. I find this more readable than recursion, and more declarative; it's just a straightforward pipeline of data transformations. The data structure is the last parameter, so that you can partially apply the value that you're looking for, like idxOfFoo = firstIndexOf "foo".
indicesOf : a -> List a -> List Int
indicesOf thing things =
things
|> List.indexedMap (,)
|> List.filter (\(idx, item) -> item == thing)
|> List.map fst
firstIndexOf : a -> List a -> Int
firstIndexOf thing things =
indicesOf thing things
|> List.minimum
|> Maybe.withDefault -1
lastIndexOf : a -> List a -> Int
lastIndexOf thing things =
indicesOf thing things
|> List.maximum
|> Maybe.withDefault -1
REPL session:
> things = ["foo", "bar", "bat", "foo", "baz"]
["foo","bar","bat","foo","baz"] : List String
>
> firstIndexOf "foo" things
0 : Int
>
> lastIndexOf "foo" things
3 : Int
>
At a glance, I think this is similar in complexity to CircuitHub's implementation in List.Extra.
Code
indexOf : a -> Array a -> Int
indexOf element array =
let
item =
array
|> Array.toIndexedList
|> List.filter (\tuple -> isElement tuple element)
|> List.head
in
case item of
Just value ->
Tuple.first value
Nothing ->
-1
isElement : (Int, a) -> a -> Bool
isElement tuple element =
Tuple.second tuple == element
Usage
list =
[ "a"
, "b"
, "c"
]
index =
list
|> Array.fromList
|> indexOf "b"

F# Finding the Missing element between 2 arrays/lists

`So, I am very new to F#. I hope the issue is simple. I have been doing research and looking around. I have an "Incomplete structured construct at or before this point in expression" error. I feel like it might be something simple or I am way off.
The objective is:
There is an array of non negative integers. A second array is
formed by shuffling the elements of the first
array and deleting a random element. Given these two arrays, find which element is missing in the second
array. Linear searching is not allowed.
let FindMiss list =
match list with
| [] ->
[]
|firstElem::otherElements ->
let rand = new Random
let shuffle (arr : 'a array) =
let array = Array.copy arr
let n = array.Length
for x in 1..n do
let i = n-x
let j = rand.Next(i+1)
let tmp = array.[i]
array.[i] <- array.[j]
array.[j] <- tmp
array
return array
array.[rand].delete
|array::list ->
let d=collections.defaultdict(int)
for num in list do
d[num] +=1
for num in array1 do
if d[num]==0 then return num
else d[num]-=1
printfn "The missing Number is: %A" (FindMiss[4;2;1;7;5;6;3;2])
The task is to reimplement List.except?
If not, just use that 'except' then:
[1;2;3] |> List.except [1;2]
Or was the task "random removal of element in list"? Then this is "the answer": https://stackoverflow.com/a/2889972/5514938
For a start, for more readability, you can take the shuffling and deleting a random element in a separate function. For arrays, they may look like this:
let Shuffle arr =
let rand = System.Random()
arr |> Array.sortBy(fun _ -> rand.Next())
let RemoveRandom arr =
let rand = System.Random()
let lng = arr |> Array.length
let index = rand.Next lng
[|0..lng - 1 |]
|> Array.choose(fun x -> if x = index then None else Some(arr.[x]))
|> Shuffle
Further define the search function:
let FindMiss arr1 arr2 =
let sum1 = arr1 |> Array.sum
let sum2 = arr2 |> Array.sum
sum1 - sum2
Example:
let first = [| 4;2;1;7;5;6;3;2 |]
first |> printfn "%A"
let second = first |> RemoveRandom
second |> printfn "%A"
FindMiss first second |> printfn "Missing value is %i"
Print:
[|4; 2; 1; 7; 5; 6; 3; 2|]
[|2; 2; 3; 7; 1; 5; 6|]
Missing value is 4
Link:
https://dotnetfiddle.net/g6wKUX

How to declare the array type in swift

So in a swift playground file, I am trying to execute the following closure:
var list = [5, 4, 3]
var Arraymultiplier = {(list:Array) -> Array in
for value in list {
value*2
return list
}
}
Arraymultiplier(list)
When I do this though, I get an error saying that the generic type Array must be referenced in <..> brackets, but when I put the brackets, I get another error.
What's the right way to declare the array type as an input and a return?
Array is a generic type, meaning that it expects to see the actual type of the members of the array to be specified within < > immediately following Array:
var arrayMultiplier = {(list: Array<Int>) -> Array<Int> in
// do whatever you want here
}
Or, in the case of arrays, there is a convenient syntax that uses the [ ] characters and omits the need for Array reference at all:
var arrayMultiplier = {(list: [Int]) -> [Int] in
// do whatever you want here
}
I'm not sure what the original code sample was trying to do, but it looks like you might have been trying to build a new array with each item multiplied by 2. If so, then you might do something like:
let inputList = [5, 4, 3]
let arrayMultiplier = { (list: [Int]) -> [Int] in
var results = [Int]() // instantiate a new blank array of `Int`
for value in list { // iterate through array passed to us
results.append(value * 2) // multiply each `value` by `2` and add it to the `results`
}
return results // return results
}
let outputList = arrayMultiplier(inputList)
Or, obviously, you could efficiently achieve similar behavior using the map function:
let outputList = inputList.map { $0 * 2 }

Array initialization in F#

How do I create and initialize an array in F# based on a given record type?
Suppose I want to create an Array of 100 record1 records.
e.g.
type record1 = {
value1:string;
value2:string
}
let myArray = Array.init 100 ?
But it appears the Array.init does not allow for this, is there a way to do this?
Edited to add:
Of course I could do something like this:
let myArray = [|for i in 0..99 -> { value1="x"; value2="y" }|]
You can use also Array.create, which creates an array of a given size, with all its elements initialized to a defined value:
let myArray = Array.create 100 {value1="x"; value2="y"}
Give a look to this list of array operations.
This should do what you need. Hope it helps.
type record1 = {
value1:string;
value2:string
}
let myArray = Array.init 100 (fun x -> {value1 = "x"; value2 = "y"})
or using Generics
let myArray = Array.init<record1> 100 (fun x -> {value1 = "x"; value2 = "y"})
Or you can create a sequence, instead of creating an array, like this:
// nItems, given n and an item, returns a sequence of item repeated n times
let rec nItems n item =
seq {
match n with
| n when n > 0 -> yield item; yield! nItems (n - 1) item
| _ -> ()
}
type Fnord =
{ foo: int }
printfn "%A" (nItems 99999999 {foo = 3})
// seq [{foo = 3;}; {foo = 3;}; {foo = 3;}; {foo = 3;}; ...]
printfn "%A" (nItems 3 3 |> Seq.toArray)
[|3; 3; 3|]
The nice thing about the sequence, instead of an array, is that it creates items as you need them, rather than all at once. And it's simple to go back and forth between sequences and arrays if you need to.

Resources