I'm having a problem with understanding how F# works. I come from C# and I think that I'm trying to make F# work like C#. My biggest problem is returning values in the correct format.
Example: Let's say I have function that takes a list of integers and an integer. Function should print a list of indexes where values from list match passed integer.
My code:
let indeks myList n = myList |> List.mapi (fun i x -> if x=n then i else 0);;
indeks [0..4] 3;;
However it returns:
val it : int list = [0; 0; 0; 3; 0]
instead of just [3] as I cannot ommit else in that statement. Also I have targeted signature of -> int list -> int -> int list and I get something else.
Same goes for problem no. 2 where I want to provide an integer and print every number from 0 to this integer n times (where n is the iterated value): example: MultiplyValues 3;; output: [1;2;2;3;3;3]
Best I could do was to create list of lists.
What am I missing when returning elements?
How do I add nothing to the return
example: if x=n then n else AddNothingToTheReturn
List.chooseandList.collectfunctions. But if you don't know they exist, it's hard to use them! Therefore, I recommend giving fsharpforfunandprofit.com/posts/list-module-functions a read. And when you're done, read the rest of the series there. That site the best single resource I've found for learning F#. - rmunn