问题
Need help with this,
Write a function, that takes 2 StraightLine and returns the intersecting point as a tuple
(x,y)
. If there is no solution, there shall be used an "Exception"Change your solution in assignment "1", so instead of using an "Exception" you need to use the option "None" if there is no solution. If there is a solution, use the
Some(x,y)
expression.Solve assignment "1" again, but define a record-type Line with brackets a and b til represent a line.
y=3x+4
is for example represented with the brackets{a=3.0; b=4.0}
.
回答1:
3.
type Line = {a:double; b:double}
let LinesIntersection x y =
if x.a <> y.a then
Some ((x.b - y.b)/(y.a - x.a), (y.a*x.b - x.a*y.b)/(y.a - x.a))
else None
let l1 = {a = 2.0; b = -3.0}
let l2 = {a = -3.0; b = 2.0}
let l3 = {a = 2.0; b = 4.0}
LinesIntersection l1 l2 |> printfn "%A"
LinesIntersection l1 l3 |> printfn "%A"
Print:
Some (1.0, -1.0)
<null>
Link: https://dotnetfiddle.net/uNcTEL
The rest do yourself. Does not work - show an attempt solutions
来源:https://stackoverflow.com/questions/32683811/f-not-sure-how-to-start