Building on what we have already the next couple should be a breeze. Problem #3 is as follows:
"Find the K'th element of a list."
We can basically simplify problem two a little bit and get the answer for three.
1 #light
2
3 let listerine k = [1 .. 40] |> fun x -> List.nth x k
4
5 printfn "Answer: %d" (listerine 27)
This statement should look very similar to problem 2 except for two distinct differences...
- We are passing a parameter k to the function
- We are no longer calculating the second to last character but rather finding the element k.
Pretty straight forward but this will fail if k is out of bounds.
Microsoft.FSharp.Core.InvalidArgumentException: nth
> at Microsoft.FSharp.Collections.ListModule.nth[T](List`1 l, Int32 n)
at <StartupCode$FSI_0018>.FSI_0018._main()
stopped due to error
We'll look at how to get around that at a later time :)
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5
Tags:
f#,
99 problems
Categories:
F#