# Add Function That Uses Single Variable to Merge Results to Itself

**URL:** <https://forums.fsharp.org/t/add-function-that-uses-single-variable-to-merge-results-to-itself/2099>\
**Category:** General\
**Tags:** beginners\
**Created:** [December 28, 2021, 7:04pm UTC](https://forums.fsharp.org/t/add-function-that-uses-single-variable-to-merge-results-to-itself/2099 "2021-12-28T19:04:35Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![Americore](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/americore/32/1031_2.png) [@Americore](https://forums.fsharp.org/u/Americore)\
**Post date:** [January 8, 2022, 3:51am UTC](https://forums.fsharp.org/t/add-function-that-uses-single-variable-to-merge-results-to-itself/2099/23 "2022-01-08T03:51:16Z")

</div>

> [@ntwilson](#):
>
> The other 3 examples give you a list of values that you can then combine with `String.Concat` .
> 
> Is that what you’re looking for?

You appear to have done a lot of emphasis on String.Concat. Will a function like an Array.min work with any of these? I’m noticing results similar to the problem that was originally identified in one of your posts regarding return single values.

---

<div class="post-metadata">

**Author:** ![ntwilson](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ntwilson/32/984_2.png) [@ntwilson](https://forums.fsharp.org/u/ntwilson)\
**Post date:** [January 8, 2022, 5:27pm UTC](https://forums.fsharp.org/t/add-function-that-uses-single-variable-to-merge-results-to-itself/2099/24 "2022-01-08T17:27:56Z")

</div>

> You appear to have done a lot of emphasis on String.Concat.

So in the F# type system, a value must have a single type, like `string` or `string[]` or `int[]`, which are all distinct. You can of course use a function to convert between those types. My confusion is you seem to want two different types from the output: `string` (like `"2;3;4;5;"`) and also `int[]` (like `[|2;3;4;5|]` which you could call `Array.min` on). Any given value can only have one of those types, and you can’t call `Array.min "2;3;4;5;"`, because `Array.min` takes an array, and `"2;3;4;5;"` is a string. But you can convert between `int[]` and `string` using functions like `String.Join`, `String.Concat`, and `String.Split`. For example, a couple functions here can turn an `int[]` into a `string`:

```auto
let convert1 (xs:int[]) : string = String.Join(";", xs |> Array.map string)
let convert2 (xs:int[]) : string = [for x in xs -> sprintf "%i;"] |> String.Concat
convert1 [|2;3;4;5|] // returns "2;3;4;5"
convert2 [| for i in "2 3 4 5".Split(' ') -> int i |] // returns "2;3;4;5;"

```

I’m using functions like these a lot because my understanding is you want both an `int[]` to call `Array.min` on _and_ a string representation with semicolons separating each int. Maybe you’re trying to make a value that acts like both things? That would not be possible.

---

<div class="post-metadata">

**Author:** ![Americore](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/americore/32/1031_2.png) [@Americore](https://forums.fsharp.org/u/Americore)\
**Post date:** [January 9, 2022, 1:14am UTC](https://forums.fsharp.org/t/add-function-that-uses-single-variable-to-merge-results-to-itself/2099/25 "2022-01-09T01:14:25Z")

</div>

Can you explain why a StringBuilder() will only work to achieve our goal like this:

```auto
open System
open System.Text

let str = "1 2 3 4 5 6"
let Spl = str.Split(' ')

let mutable builder = StringBuilder()

for i in 1..Spl.Length - 2 do
  builder.Append(sprintf "%s;" Spl.[i]) |> ignore

let result = string builder
printf "%A" result

```

And not like this:

```auto
open System
open System.Text

let str = "1 2 3 4 5 6"
let Spl = str.Split(' ')
for i in 1..Spl.Length - 2 do    

let v = Spl.[i]

let mutable builder = StringBuilder()

builder.Append(sprintf "%s;" v) |> ignore

let result = string builder
printf "%s" result

```

---

<div class="post-metadata">

**Author:** ![ntwilson](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ntwilson/32/984_2.png) [@ntwilson](https://forums.fsharp.org/u/ntwilson)\
**Post date:** [January 9, 2022, 1:41am UTC](https://forums.fsharp.org/t/add-function-that-uses-single-variable-to-merge-results-to-itself/2099/26 "2022-01-09T01:41:25Z")

</div>

In your second code snippet, unfortunately none of the indents came through (F# is a whitespace-sensitive language, so the meaning changes depending on which lines were indented). If you meant

```auto
open System
open System.Text

let str = "1 2 3 4 5 6"
let Spl = str.Split(' ')
for i in 1..Spl.Length - 2 do    

  let v = Spl.[i]

  let mutable builder = StringBuilder()

  builder.Append(sprintf "%s;" v) |> ignore

let result = string builder
printf "%s" result

```

then it doesn’t compile because a for loop creates its own scope. When you define `let mutable builder = StringBuilder()` inside the loop, it only exists inside the loop. You have to define it outside the loop for it to be accessible in the final 2 lines.  
If you meant

```auto
open System
open System.Text

let str = "1 2 3 4 5 6"
let Spl = str.Split(' ')
for i in 1..Spl.Length - 2 do    

  let v = Spl.[i]

  let mutable builder = StringBuilder()

  builder.Append(sprintf "%s;" v) |> ignore

  let result = string builder
  printf "%s" result

```

then it compiles, but the builder is sort of unnecessary. You’re not actually “pushing” values to the string in each iteration, in each iteration you make a new StringBuilder which only has one value in it. It could just be simplified to

```auto
open System
open System.Text

let str = "1 2 3 4 5 6"
let Spl = str.Split(' ')
for i in 1..Spl.Length - 2 do    

  let v = Spl.[i]

  let result = sprintf "%s;" v
  printf "%s" result

```

and you can verify you get the same result. There’s nothing wrong with that, it’s just that you’re “pushing” values directly to the console, and once you leave the loop you don’t have access to any those values. And inside the loop, you only have access to each individual element (not all of the processed elements so far). But if all you need to do is print those values to the console, that works just fine. Though it would be more idiomatic and succinct to replace the whole for loop with just the single line

```auto
printf "%s;" (String.Join(";", Spl.[1 .. Spl.Length - 2]))

```

---

<div class="post-metadata">

**Author:** ![Americore](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/americore/32/1031_2.png) [@Americore](https://forums.fsharp.org/u/Americore)\
**Post date:** [January 9, 2022, 4:40am UTC](https://forums.fsharp.org/t/add-function-that-uses-single-variable-to-merge-results-to-itself/2099/28 "2022-01-09T04:40:34Z")

</div>

> [@ntwilson](#):
>
> leave the loop you don’t have access to any those values.

Do you know of any accumulator helper functions that are more certain to work here? Resolve several single values pushed into a long system of code in the middle of a script not containing after they’ve been parsed and map, rejoin, or contain all the values for Continuation?

---

<div class="post-metadata">

**Author:** ![ntwilson](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ntwilson/32/984_2.png) [@ntwilson](https://forums.fsharp.org/u/ntwilson)\
**Post date:** [January 9, 2022, 5:38pm UTC](https://forums.fsharp.org/t/add-function-that-uses-single-variable-to-merge-results-to-itself/2099/29 "2022-01-09T17:38:38Z")

</div>

Yes, all 4 of the code examples from my post on 2022-01-07 feature single values being pushed into a collection from the middle of a for loop, where that collection can then be used from outside the loop. (That would be the 2 forms of list comprehensions, the `ResizeArray` and the `StringBuilder`)

---

<div class="post-metadata">

**Author:** ![Americore](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/americore/32/1031_2.png) [@Americore](https://forums.fsharp.org/u/Americore)\
**Post date:** [January 10, 2022, 12:08am UTC](https://forums.fsharp.org/t/add-function-that-uses-single-variable-to-merge-results-to-itself/2099/30 "2022-01-10T00:08:31Z")

</div>

> [@ntwilson](#):
>
> Yes, all 4 of the code examples from my post on 2022-01-07 feature single values being pushed into a collection from the middle of a for loop, where that collection can then be used from outside the loop. (That would be the 2 forms of list comprehensions, the `ResizeArray` and the `StringBuilder` )

Like this?

```auto
let str = "1 2 3 4 5 6"
let Spl = str.Split(' ')
for i in 1..Spl.Length - 2 do    

  let v = Spl.[i]

//DU goes here//

//results pipe out here//

//join, map, or collection happens here//

//do something function using int or any arithmetic for Continuation//

```

---

<div class="post-metadata">

**Author:** ![ntwilson](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ntwilson/32/984_2.png) [@ntwilson](https://forums.fsharp.org/u/ntwilson)\
**Post date:** [January 10, 2022, 2:50am UTC](https://forums.fsharp.org/t/add-function-that-uses-single-variable-to-merge-results-to-itself/2099/31 "2022-01-10T02:50:19Z")

</div>

How’s this?

```auto
open System
type NumbersYouCareAbout = EvenNumbers | OddNumbers
let whatNumbersDoYouCareAbout = EvenNumbers
let str = "1 2 3 4 5 6"
let Spl = str.Split(' ')
let processedNumbers : float list = 
  [ 
    for i in 1..Spl.Length - 2 do
      let v : float = float Spl.[i]
      match whatNumbersDoYouCareAbout with
      | OddNumbers -> 
        if v % 2.0 <> 0.0 then yield v
      | EvenNumbers -> 
        if v % 2.0 = 0.0 then yield v
  ]

let result = List.average processedNumbers
printfn "the average of [%s] is %f" (String.Join(";", processedNumbers |> List.map string)) result

```

---

<div class="post-metadata">

**Author:** ![Americore](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/americore/32/1031_2.png) [@Americore](https://forums.fsharp.org/u/Americore)\
**Post date:** [January 10, 2022, 3:22am UTC](https://forums.fsharp.org/t/add-function-that-uses-single-variable-to-merge-results-to-itself/2099/32 "2022-01-10T03:22:32Z")

</div>

> [@ntwilson](#):
>
> How’s this?

I might be confusing you. So, this is my DU now:

```auto
open System
open System.Collections.Generic

let Data = "1 2 3" 
let Res = Data.ToString()
let Spl = (Res:string).Split(' ')

for i in 0..Spl.Length - 1 do    
let v = Spl.[i]
let k = i

let Find L =
    let dic = new Dictionary<string,string>()
    dic.Add(k.ToString(),v)
    
    let mutable Div=""
   
    for obj in L do
        let k,v = dic.TryGetValue(obj)      
        Div <-if k then v else Div            
    Div
   
Find ["0";"1";"2"] |> fun eq ->
    eq
     
    if (eq.Length >= 1) then

    printf "%A" eq

```

Rejoin, map, or apply collection so that continuation and arithmetic eg. Array.min is possible after using DU key to list designated table related values.

---

<div class="post-metadata">

**Author:** ![ntwilson](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ntwilson/32/984_2.png) [@ntwilson](https://forums.fsharp.org/u/ntwilson)\
**Post date:** [January 10, 2022, 4:31am UTC](https://forums.fsharp.org/t/add-function-that-uses-single-variable-to-merge-results-to-itself/2099/33 "2022-01-10T04:31:19Z")

</div>

Okay, what about this

```auto
open System
open System.Collections.Generic

let Data = "1 2 3" 
let Res = Data.ToString()
let Spl = (Res:string).Split(' ')

let allEqValues = 
  [
    for i in 0..Spl.Length - 1 do    
      let v = Spl.[i]
      let k = i

      let Find L =
          let dic = new Dictionary<string,string>()
          dic.Add(k.ToString(),v)
          
          let mutable Div=""
        
          for obj in L do
              let k,v = dic.TryGetValue(obj)      
              Div <-if k then v else Div            
          Div
      
      let eq = Find ["0";"1";"2"] 
      if (eq.Length >= 1) then 
        printf "%A" eq
        yield eq
  ]

let min = List.min (List.map int allEqValues)

```

As an aside, this is a fairly imperative (and thus verbose) implementation of the `find` function. A more idiomatic F# solution might look more like

```auto
let find L = if L |> Seq.contains k then v else ""

```

---

<div class="post-metadata">

**Author:** ![Americore](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/americore/32/1031_2.png) [@Americore](https://forums.fsharp.org/u/Americore)\
**Post date:** [January 10, 2022, 5:50am UTC](https://forums.fsharp.org/t/add-function-that-uses-single-variable-to-merge-results-to-itself/2099/34 "2022-01-10T05:50:00Z")

</div>

> [@ntwilson](#):
>
> `let find L = if L |> Seq.contains k then v else ""`

As a principled substitute for IDictionary? Thanks heck! You’re the greatest, ntwilson!!!

[Previous page](https://forums.fsharp.org/t/add-function-that-uses-single-variable-to-merge-results-to-itself/2099.md?page=1)
