Serie: haskell
haskell
32 righe
· Aggiornato 2026-02-03
Area.hs
haskell/Area.hs
module Area
( isValidTriangle
, areaTriangleSafe
) where
-- | Check triangle validity (positive sides + strict triangle inequality).
-- >>> isValidTriangle 1 2 4
-- False
-- >>> isValidTriangle 3 4 5
-- True
isValidTriangle :: Double -> Double -> Double -> Bool
isValidTriangle a b c =
let pos x = x > 0
in all pos [a,b,c]
&& a + b > c
&& a + c > b
&& b + c > a
-- | Safe area using Heron's formula; returns Nothing for invalid triangles.
-- >>> areaTriangleSafe 3 4 5
-- Just 6.0
-- >>> areaTriangleSafe 1 2 4
-- Nothing
areaTriangleSafe :: Double -> Double -> Double -> Maybe Double
areaTriangleSafe a b c
| not (isValidTriangle a b c) = Nothing
| otherwise = Just area
where
s = (a + b + c) / 2
-- guard against tiny negative due to floating-point rounding
radicand = max 0 (s * (s - a) * (s - b) * (s - c))
area = sqrt radicand
Articoli correlati
haskell
haskell
Aggiornato 2026-02-03
Adding.hs
Adding.hs — haskell source code from the haskell learning materials (haskell/Adding.hs).
Leggi l'articolo →
haskell
haskell
Aggiornato 2026-02-03
Chess.hs
Chess.hs — haskell source code from the haskell learning materials (haskell/Chess.hs).
Leggi l'articolo →
haskell
haskell
Aggiornato 2026-02-03
ChessGraphics.hs
ChessGraphics.hs — haskell source code from the haskell learning materials (haskell/ChessGraphics.hs).
Leggi l'articolo →
haskell
haskell
Aggiornato 2026-02-03
Examples.hs
Examples.hs — haskell source code from the haskell learning materials (haskell/Examples.hs).
Leggi l'articolo →
haskell
haskell
Aggiornato 2026-02-03
Fibonacci.hs
Fibonacci.hs — haskell source code from the haskell learning materials (haskell/Fibonacci.hs).
Leggi l'articolo →
haskell
haskell
Aggiornato 2026-02-03
GPACalculator.hs
GPACalculator.hs — haskell source code from the haskell learning materials (haskell/GPACalculator.hs).
Leggi l'articolo →