我是Haskell的新手.我从我的任务中得到了这个问题.它要我让这段代码工作:
area_of_triangle :: Float
-> Float
-> Float
-> Maybe Float
Run Code Online (Sandbox Code Playgroud)
我知道如何做到这一点Maybe; 就像是:
area_of_triangle :: Float -> Float -> Float -> Float
area_of_triangle a b c = sqrt(s*(s-a)*(s-b)*(s-c))
where
s = (a+b+c)/2
Run Code Online (Sandbox Code Playgroud)
我想要的是if area_of_triangle=0.0,return Nothing(因为这样的三角形不存在).但我不知道怎么写这个.
如果每对长度的总和大于另一长度,则三个长度只能形成三角形.如果不是这样,请返回Nothing.否则,您可以返回Just a,a您使用原始公式计算的长度在哪里.
area_of_triangle :: Float -> Float -> Float -> Float
area_of_triangle a b c = sqrt(s*(s-a)*(s-b)*(s-c))
where
s = (a+b+c)/2
area :: Float -> Float -> Float -> Maybe Float
area a b c
| ??? = Nothing
| ??? = Nothing
| ??? = Nothing
| otherwise = Just (???)
Run Code Online (Sandbox Code Playgroud)
我把它作为练习来确定哪些布尔表达式替换前三个???s,以及用什么替换最后一个???.
| 归档时间: |
|
| 查看次数: |
1092 次 |
| 最近记录: |