F# : not sure how to start [closed]

China☆狼群 提交于 2019-12-14 03:33:41

问题


Need help with this,

  1. 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"

  2. 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.

  3. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!