F#:不确定如何开始 [英] F# : not sure how to start

查看:69
本文介绍了F#:不确定如何开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要帮助,

  1. 编写一个函数,该函数采用2条StraightLine,并将相交点作为元组(x,y)返回.如果没有解决方案,则应使用例外"

  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"

更改分配"1"中的解决方案,因此如果没有解决方案,则需要使用选项"None"来代替"Exception".如果有解决方案,请使用Some(x,y)表达式.

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.

再次解决分配"1",但定义一个记录类型Line,其中括号a和b til代表一行.例如,y=3x+4用方括号{a=3.0; b=4.0}表示.

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

推荐答案

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"

打印:

Some (1.0, -1.0)
<null>

链接: https://dotnetfiddle.net/uNcTEL

其余的事由你自己做.不起作用-显示尝试解决方案

The rest do yourself. Does not work - show an attempt solutions

这篇关于F#:不确定如何开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆