I am curious why when I run this, the function "parsenumber" gets outputted as a Seq and not just an int.
|> Seq.map (fun ((number,income,sex,house),data) ->
let parsenumber = number |> Seq.skip 9 |> Seq.take 5
let numberofpets = data |> Seq.map (fun row -> (1.0 - float row.pets))
((parsenumber,income,sex,house),numberofpets)
This is the result:
(<seq>, "30050", "Male", "Yes")
(<seq>, "78000", "Female", "No")
How can I change this so it outputs the number and not <seq>.
With Seq.skip and Seq.take, I am trying to skip the first 9 integers of each observation in number and return the last 5.
RAW CSV DATA:
10000000001452,30050,Male,Yes
10000000001455,78000,Female,No
What I want as a result:
('01452','30050','Male','Yes')
('01455','78000','Female','No')
What I am actually getting:
(<seq>, "30050", "Male", "Yes")
(<seq>, "78000", "Female", "No")
I need to not have as an output, and the actual number instead.
When you say, "I am trying to skip the first 9 integers of each observation in number and return the last 5", did you mean "digits" rather than "integers"? I.e., is number a string originally? Then you should use number.Substring(9, 5) instead of Seq.skip and Seq.take. The Seq.skip and Seq.take functions are defined as returning sequences — that's what they're for. When you interpret a string as a sequence, it returns a sequence of characters. If you use the .Substring method, it returns a string.
BTW, if you want to use .Substring, you'll need to tell F# what type you expect number to be: calling methods of a parameter is one place where F#'s type inference can't figure out what type you have. (Because in theory, you could have defined your own type with a .Substring method and meant to call that type). To explicitly declare the type of the number parameter, you'd use a colon and the type name, so that fun ((number,income,sex,house),data) -> would become fun ((number : string, income, sex, house), data) ->. So your entire Seq.map expression would become:
|> Seq.map (fun ((number : string, income, sex, house), data) ->
let parsenumber = number.Substring(9, 5)
let numberofpets = data |> Seq.map (fun row -> (1.0 - float row.pets))
((parsenumber, income, sex, house), numberofpets)
Also, parsenumber isn't a function, so that's not a good name for it. Possibly parsednumber would be better, though if I understood more about what you're trying to do then there's probably an even better suggestion.
Related
I am looking for a type similar to sequences in F# where indices could be big integers, rather that being restricted to int. Does there exist anything like this?
By "big integer indices" I mean a type which allows for something equivalent to that:
let s = Seq.initInfinite (fun i -> i + 10I)
The following will generate an infinite series of bigints:
let s = Seq.initInfinite (fun i -> bigint i + 10I)
What i suspect you actually want though is a Map<'Key, 'Value>.
This lets you efficiently use a bigint as an index to look up whatever value it is you care about:
let map =
seq {
1I, "one"
2I, "two"
3I, "three"
}
|> Map.ofSeq
// val map : Map<System.Numerics.BigInteger,string> =
// map [(1, "one"); (2, "two"); (3, "three")]
map.TryFind 1I |> (printfn "%A") // Some "one"
map.TryFind 4I |> (printfn "%A") // None
The equivalent of initInfinite for BigIntegers would be
let inf = Seq.unfold (fun i -> let n = i + bigint.One in Some(n, n)) bigint.Zero
let biggerThanAnInt = inf |> Seq.skip (Int32.MaxValue) |> Seq.head // 2147483648
which takes ~2 min to run on my machine.
However, I doubt this is of any practical use :-) That is unless you start at some known value > Int32.MaxValue and stop reasonably soon (generating less than Int32.MaxValue items), which then could be solved by offsetting the BigInt indexes into the Int32 domain.
Theoretically you could amend the Seq module with functions working with BigIntegers to skip / window / ... an amount of items > Int32.MaxValue (e.g. by repeatedly performing the corresponding Int32 variant)
Since you want to index into a sequence, I assume you want a version of Seq.item that takes a BigInteger as index. There's nothing like that built into F#, but it's easy to define your own:
open System.Numerics
module Seq =
let itemI (index : BigInteger) source =
source |> Seq.item (int index)
Note that no new type is needed unless you're planning to create sequences that are longer than 2,147,483,647 items, which would probably not be practical anyway.
Usage:
let items = [| "moo"; "baa"; "oink" |]
items
|> Seq.itemI 2I
|> printfn "%A" // output: "oink"
I'm trying to read two integers which are going to be taken as input from the same line. My attempt so far:
let separator: char =
' '
Console.ReadLine().Split separator
|> Array.map Convert.ToInt32
But this returns a two-element array and I have to access the individual indices to access the integers. Ideally, what I would like is the following:
let (a, b) =
Console.ReadLine().Split separator
|> Array.map Convert.ToInt32
|> (some magic to convert the two element array to a tuple)
How can I do that?
I'm afraid there's no magic. You have to explicitly convert into a tuple
let a, b =
Console.ReadLine().Split separator
|> Array.map int
|> (fun arr -> arr.[0], arr.[1])
Edit: you can use reflection as #dbc suggested but that's slow and probably overkill for what you're doing.
I'm working on a homework assignment. And I'm trying to learn F# so I don't want any shortcuts besides using basic things like List.Map or lambdas or something.
I'm trying to process a list of tuples, but I'm having trouble accessing the tuples in the list.
I want to take the list of tuples, add up the numbers in each tuple, and return that number, printing it out each time.
let listTup = [(2,3,4); (4,5,6); (6,7,8)]
let getSum (a,b,c) =
a+b+c
let rec printSum tpList =
let total = 0
match tpList with
| [] -> total //return 0 if empty list
| hd::tl ->
print (getSum hd)
The first thing you want to do is map your tuples through the getSum function. This can be done very simply by piping the list of tuples into List.map getSum. Then you want to print each element in the list, so you pipe the result of List.map getSum into List.iter with the function printfn "%d". This works because of the functions having curried parameters. printfn "%d" applies the "%d" parameter to printfn and returns a function taking an integer, which it then prints. The whole thing would look like this:
let listTup = [(2,3,4); (4,5,6); (6,7,8)]
let getSum (a,b,c) =
a + b + c
let printSum tpList =
tpList |> List.map getSum |> List.iter (printfn "%d")
This prints:
9
15
21
We can even simplify the function further if we take advantage of function composition (the >> operator). Notice that printSum takes tpList as its parameter, and then just uses it as input to two functions that are pipelined together. Since pipelining just takes the output of one function and passes it as the last parameter of another function, all we really need to do is compose the function List.map getSum, which takes a list of int 3-tuples and returns a list of ints with List.iter (printfn "%d"), which takes a list of ints and returns unit. That would look like this:
let printSum = List.map getSum >> List.iter (printfn "%d")
This will print the same results, but is a simpler way of expressing the function.
F# has imperative loops as well. In this case I think an imperative loop matches the problem most idiomatically.
let listTup = [(2,3,4); (4,5,6); (6,7,8)]
for a,b,c in listTup do
let sum = a + b + c
printfn "%d" sum
The problem is (source)...
The four adjacent digits in the 1000-digit number that have the greatest
product are 9 × 9 × 8 × 9 = 5832.
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
Find the thirteen adjacent digits in the 1000-digit number that have
the greatest product. What is the value of this product?
I have the following F#...
let largestProduct n (s : string) =
[ for i in [0..(s.Length - n)] do yield s.[i..(i + n - 1)]]
|> Seq.map (fun s -> s, s |> Seq.fold (fun p c -> p * (int (string c))) 1)
|> Seq.maxBy snd
You pass the number of digits, and the 1000 digit number as a string. The first line produces a sequence of n-character strings, which are piped into the second line where the product of the digits is calculated. This is wrapped in a tuple with the n-character string, so I can see which set of n characters produced the highest product. The last line gets the maximum product.
If I run this as follows...
largestProduct 4 nStr
...where nStr is the 1000-digit number as a string, it produces the following...
("9989", 5832)
...which is correct. However, if I change the number to 13, to solve the actual problem, it gives me...
("9781797784617", 2091059712)
...which apparently is wrong.
Anyone any idea why my code doesn't work? I've tried it for various small values of n, and it looks like it's working there. I've also tried it on shorter strings, and it seems to work fine.
This exercise causes an Int32 to overflow. The arbitrary-length type bigint solves this for a generally sufficient range of inputs. For example:
let digitsToProduct inp =
inp |> Seq.map (string >> bigint.Parse)
|> Seq.fold (*) 1I
let largestProduct n : (seq<char> -> bigint) =
Seq.windowed n >> Seq.map digitsToProduct >> Seq.max
Edit: note that largestProduct takes a second argument: a string (or any char sequence) of the 1000 digits.
What can be learned from this?
This is a fundamental kind of problem that is worth thinking about. As a rule of thumb, a function should be correct or fail, at least for reasonable inputs. I would argue that, in any context where a developer might make such mistakes, the answer to just use a 64-bit integer is borderline incorrect. After all, it will still fail silently on inputs that are too large.
If you want to use a 32-bit or 64-bit integer for such a function, validate your inputs!
For example, a rough validation could be:
// 32b version
if n > 9 then invalidArg "n" "number of digits too large for Int32."
// 64b version
if n > 19L then invalidArg "n" "number of digits too large for Int64."
This would cause your program to fail properly instead of silently producing nonsensical results.
As you've figured, using int64 solves the problem.
The way I read the assignment, you don't have to return the digits that cause the largest product; only the product itself is required. With that requirement, the implementation is easy:
let largestProduct n : (string -> int64) =
Seq.map (string >> System.Int64.Parse)
>> Seq.windowed n
>> Seq.map (Array.fold (*) 1L)
>> Seq.max
If you want the sequence of digits as well, that's also easy:
let largestProductAndTheDigitsThatProduceIt n : (string -> string * int64) =
Seq.map (string >> System.Int64.Parse)
>> Seq.windowed n
>> Seq.map (fun is -> System.String.Concat is , Array.fold (*) 1L is)
>> Seq.maxBy snd
FSI:
> largestProductAndTheDigitsThatProduceIt 4 nStr;;
val it : string * int64 = ("9989", 5832L)
> largestProductAndTheDigitsThatProduceIt 13 nStr;;
val it : string * int64 = ("5576689664895", 23514624000L)
Whilst searching for some inspiration, I came across a question where someone had the same problem.
The answer was nothing more complex than the fact that the multiplication overflowed the capacity of a 32-bit integer. When I altered the code to use int64, it gave the right answer...
let largestProductInt64 (n : int64) (s : string) =
[ for i in [0L..((int64 s.Length) - n)] do yield s.[(int i)..int(i + n - 1L)]]
|> Seq.map (fun s -> s, s |> Seq.fold (fun p c -> p * (int64 (int (string c)))) 1L)
|> Seq.maxBy snd
Shame, as the code isn't as neat and clean, but it works.
Thanks to #kvb who made the same point in a comment before I had chance to post my own findings.
i'm new to F# and i've come up with a solution to a fairly simple question, which is, given a string of numbers, I need to multiple each digit by two, then get the sum of these digits. For example, the string "123456" should equate to 24
Here is what I was able to come up with
let input = "123456"
|> Seq.map (string >> int)
|> Seq.map (fun(x) -> x * 2)
|> Seq.map (int >> string)
|> String.concat String.Empty
|> Seq.map (string >> int)
|> Seq.sum
My question is, is there any substantial changes I could make, to make my solution more condensed and more efficient? Any help will be greatly appreciated
You can compact your code by applying some properties.
fun x -> x * 2 is
fun x -> (*) x 2 (operator as a function)
fun x -> (*) 2 x (commutative)
(*) 2 (eta-reduction)
map composition is the same as the composition of the functions to map, your first 3 maps could be written as:
Seq.map (string >> int >> (*) 2 >> string)
map and then concat is collect
map and then sum is sumBy
So your code could be:
"123456"
|> String.collect (string >> int >> (*) 2 >> string)
|> Seq.sumBy (string >> int)
I understand that this might be a toy example, but I think that it is actually quite hard to write it in a readable way. I had to run your code line-by-line before I actually understood what is going on!
You can definitely contract it into quite short expression using point-free style (as Gustavo's answer shows), but I would probably not go that far - because you might need to be able to read & understand the code later!
An alternative way would be to use sequence expressions and write something like this:
[ for c in "123456" do
// Get the number, multiply it & yield its characters
let number = int (string c)
yield! string (number * 2) ]
// Sum the numbers (converting char to int via a string)
|> Seq.sumBy (string >> int)
This is a bit shorter than your original version, but it is still (I think) fairly understandable.
In terms of efficiency, instead of converting each character in the string into a string in its own right just so you can then convert it to an int...
|> Seq.map (string >> int)
I would convert the character directly to a number:
|> Seq.map (Char.GetNumericValue >> int)
This results in less garbage to collect, and although I haven't benchmarked it, I would expect it to be faster.
Like Tomas commented I've not considered the digit-requirement in my original post. So heres my corrected version:
"123456"
|> Seq.map (string >> int) // Convert to int sequence
|> Seq.collect(fun x -> string(x*2)) // Perform multiplication,
// convert to string and collect the digits as chars
|> Seq.sumBy (string >> int) // Convert the chars to string and than to integer
// to sum them correctly
It is more or less the same what Gustavo posted.