Tres iguales
Definir la función
1 |
tresIguales :: Int -> Int -> Int -> Bool |
tal que (tresIguales x y z)
se verifica si los elementos x
, y
y z
son iguales. Por ejemplo,
1 2 |
tresIguales 4 4 4 == True tresIguales 4 3 4 == False |
Soluciones
A continuación se muestran las soluciones en Haskell y las soluciones en Python.
1 2 |
tresIguales :: Int -> Int -> Int -> Bool tresIguales x y z = x == y && y == z |
El código se encuentra en GitHub.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
from hypothesis import given, strategies as st # 1ª definición def tresIguales1(x: int, y: int, z: int) -> bool: return x == y and y == z # 2ª definición def tresIguales2(x: int, y: int, z: int) -> bool: return x == y == z # La propiedad de equivalencia es @given(st.integers(), st.integers(), st.integers()) def test_equiv_tresIguales(x, y, z): assert tresIguales1(x, y, z) == tresIguales2(x, y, z) # La comprobación es # src> poetry run pytest -q tres_iguales.py # 1 passed in 0.16s |
El código se encuentra en GitHub.
Comentarios
- La conjunción de
x
ey
se calcula- en Haskell, con
x && y
y - en Python, con
x and y
.
- en Haskell, con
- En Python,
x == y == z
es equivalente ax == y and y == z
.