Menu Close

Subrayado de un carácter

Definir el procedimiento

   subraya :: String -> Char -> IO ()

tal que (subraya cs c) escribe la cadena cs y debajo otra subrayando las ocurrencias de c. Por ejemplo,

   λ> subraya "Salamanca es castellana" 'a'
   Salamanca es castellana
    ^ ^ ^  ^     ^     ^ ^
   λ> subraya "Salamanca es castellana" 'n'
   Salamanca es castellana
         ^              ^ 
   λ> subraya "Salamanca es castellana" ' '
   Salamanca es castellana
            ^  ^

Soluciones

-- 1ª definición
subraya :: String -> Char -> IO ()
subraya cs c = do
  putStrLn cs
  putStrLn (subrayado cs c)
 
subrayado :: String -> Char -> String
subrayado cs c = map procesa cs
  where procesa x | x == c    = '^'
                  | otherwise = ' '
 
-- 2ª definición
subraya2 :: String -> Char -> IO ()
subraya2 cs c = 
  mapM_ putStrLn [cs, subrayado cs c]

4 soluciones de “Subrayado de un carácter

  1. enrnarbej
    subraya :: String -> Char -> IO()
    subraya xs a = putStrLn (xs ++ "n" ++ cadena xs a)
     
    cadena :: String -> Char -> String
    cadena []     _ = []
    cadena (x:xs) a | a == x    = '^' : cadena xs a
                    | otherwise = ' ' : cadena xs a
  2. eliguivil
    subraya :: String -> Char -> IO ()
    subraya xs c =
      do putStrLn xs
         putStrLn $ sustituye2 xs c
      where
         {-1a def. de sustituye-}
         sustituye1 []     _ = []
         sustituye1 (x:xs) c
           | x == c    = '^':sustituye1 xs c
           | otherwise = ' ':sustituye1 xs c
         {-2a def. de sustituye-}
         sustituye2 xs c = foldr (s -> if s == c
                                        then (:) '^'
                                        else (:) ' ') [] xs
  3. Juanjo Ortega (juaorture)
    subraya :: String -> Char -> IO ()
    subraya xs c = do
      putStrLn xs
      aux xs c
      where aux []     _ = putChar 'n' 
            aux (x:xs) c = if x == c
                           then do putChar '^'
                                   aux xs c
                           else do putChar ' '
                                   aux xs c
  4. albcercid
    subraya :: String -> Char -> IO ()
    subraya cs x = do putStrLn cs
                      putStrLn (sub cs x)
    sub [] x = []
    sub (x:xs) a | x == a = '^':sub xs a
                 | otherwise = ' ':sub xs a

Escribe tu solución

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