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.
Related
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 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.
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've got an array of strings, call it a, where each individual string represents a number. I also have a function f : int -> int -> int, which I want to use to "reduce a to a single number". I would like to write:
a |> Array.reduce (fun x y -> f (int32 x) (int32 y))
but this does not work, because the type of "reduce" forbids me from returning integers from f (since a is an array of strings)
Is there a functional way to make this work without having to return a
string from f or casting the string array to an int array beforehand?
using Array.reduce mapping first
If you don't want to adapt your f to handle strings, and you want to use Array.reduce then yeah I guess you should convert first (and to be honest: it seems easier than doing it manually with your wrapper-lambda) - so why not just use
a
|> Array.map int32
|> Array.reduce f
instead?
if you are concerned with the overhead of producing an intermediate Array you can always switch Array. with Seq. to to it lazily:
a |> Seq.map int32 |> Seq.reduce f
using Array.fold
aside from that you can always fold to your hearts desire:
a |> Array.fold (fun n s -> n + int32 s) 0
so you might call this more functional or not ;)
i'm new to F# so please take it easy on me :)
I wish to convert a string to a list of ints, however, the code i've written returns a list of ints in their ASCII representation.
let input = "123456"
|> Seq.map int
|> Seq.map(fun(x) -> x * 2)
returns
98
100
102
104
106
108
I can't seem to find an int.Parse alternative in F#. Any help would be great
Being a .NET language, you can use the Char.GetNumericValue method directly
open System
let input = "123456"
|> Seq.map Char.GetNumericValue
|> Seq.map(fun(x) -> x * 2)
EDIT:
Apparently Int32.Parse does not accept a single Char. Updated my answer accordingly.
Yet another way:
"123456"
|> Seq.map (string >> int)
|> Seq.map ((*) 2)
Whenever you need to map a set of characters to their integer index in an ordered set, you can simply subtract the index of the first value in the set (in this case '1'), and thus avoid any library functions:
"123456"
|> Seq.map (fun c -> int c - int '1')
|> Seq.map (fun(x) -> x * 2);;
val it : seq<int> = seq [0; 2; 4; 6; ...]
The second mapped function can also be made point-free as:
"123456"
|> Seq.map (fun c -> int c - int '1')
|> Seq.map ((*) 2);;