Menu Close

Máximo de una función

Enunciado

-- Se considera la siguiente función
--    g :: Integer -> Integer
--    g n = if n < 10 then n*n else n
-- 
-- Definir la función 
--    max_g :: Integer -> Integer
-- tal que (max_g n) es el punto i del intervalo [0,n] donde g alcanza
-- el máximo de sus valores, si n es positivo y 0 en caso contrario. Por
-- ejemplo,
--    max_g (-7)  ==  0
--    max_g 7     ==  7
--    max_g 14    ==  9
--    max_g 84    ==  84
-- 
-- Comprobar con QuickCheck que la función max_g es equivalente a la
-- función f definida por  
--    f :: Integer -> Integer
--    f n | n < 0             = 0
--        | n >= 10 && n < 81 = 9
--        | otherwise         = n
--
-- Nota: Se piden dos definiciones de max_g, una por comprensión y otra
-- por recursión.

Soluciones

import Test.QuickCheck
 
g :: Integer -> Integer
g n = if n < 10 then n*n else n
 
-- 1ª definición (por comprensión):
max_gC :: Integer -> Integer
max_gC n | n < 0     = 0 
         | otherwise = snd (maximum [(g i,i) | i <- [0..n]])
 
-- 2ª definición (por recursión):
max_gR :: Integer -> Integer
max_gR n | n < 0      = 0
         | g m > g n  = m 
         | otherwise  = n
         where m = max_gR (n - 1)
 
f :: Integer -> Integer
f n | n < 0             = 0
    | n >= 10 && n < 81 = 9
    | otherwise         = n
 
-- La propiedad con max_gR es
prop_max_gR n = max_gR n == f n
 
-- La comprobación es
--    ghci> quickCheck prop_max_gR
--    +++ OK, passed 100 tests.
 
-- La propiedad con max_gC es
prop_max_gC n = max_gC n == f n
 
-- La comprobación es
--    ghci> quickCheck prop_max_gC
--    +++ OK, passed 100 tests.
Avanzado

3 soluciones de “Máximo de una función

  1. Julián Galindo
    max_gC :: Integer -> Integer
    max_gC n 
        | n < 0 = 0
        | otherwise = last [l | (l,m) <- valores n,
                                 m == maximum [r | (s,r) <- valores n]]
     
    valores n =  zip [0..n] [g y | y <- [0..n]]
     
    max_gR :: Integer -> Integer
    max_gR n | n < 0 = 0
             | n < 10 || g n >= 81 = n
             | otherwise = max_gR (n-1)
     
    -- Comprobar con QuickCheck que la función max_g es equivalente a la
    -- función f definida por  
     
    f :: Integer -> Integer
    f n | n < 0             = 0
        | n >= 10 && n < 81 = 9
        | otherwise         = n
     
    prop_max_gC n = max_gC n == f n
    prop_max_gR n = max_gR n == f n
     
    -- comprobaciones
    --  *Main> quickCheck prop_max_gC
    --  +++ OK, passed 100 tests.
     
    --  *Main> quickCheck prop_max_gR
    --  +++ OK, passed 100 tests.
  2. Jesús Navas Orozco

    — Por comprensión:

    max_g :: Integer -> Integer
    max_g n | n <= 0    = 0
            | otherwise = last [x| x <- [0..n], g x == m n]
     
    m :: Integer -> Integer
    m n = maximum [g y| y <- [0..n]]
     
    -- Comprobar con QuickCheck que la función max_g es equivalente a la
    -- función f definida por :
     
    f :: Integer -> Integer
    f n | n < 0             = 0
        | n >= 10 && n < 81 = 9
        | otherwise         = n
     
    prop_g_f :: Integer -> Bool
    prop_g_f n = max_g n == f n
     
    -- La comprobación es: 
    --    *Main> quickCheck prop_g_f
    --    +++ OK, passed 100 tests.
  3. María Ruiz
    g :: Integer -> Integer
    g n = if n < 10 then n*n else n
     
    maximoR f a b = aux a (a+1) (f a)
        where aux y x m | x > b     = y
                        | f x > m   = aux x (x+1) (f x)
                        | otherwise = aux y (x+1) m
     
    max_gR n = maximoR g 0 n
     
    maximoC f a b = head [x | x <-[a..b], f x == m]
        where m = maximum [f x | x <-[a..b]]
     
    max_gC n | n < 0     = 0
             | otherwise = maximoC g 0 n
     
    f :: Integer -> Integer
    f n | n < 0             = 0
        | n >= 10 && n < 81 = 9
        | otherwise         = n
     
     
    prop n = max_gR n == y && max_gC n == y
        where y = f n
     
    -- quickCheck prop
    -- +++ OK, passed 100 tests.

Escribe tu solución

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.