How can I search for the types of length, take, drop, splitAt, !! and replicate all in one go, without entering in :t length, :t take, :t drop, etc, for all of these functions?
ghci includes a :def command. The command has the form :def <name> <function>, where <name> is the new command to define, and <function> is a Haskell function of type String -> IO String saying how to convert arguments to the new command into an existing chain of commands. We can use this to our advantage: we'll make a new command, :manyt, which takes a list of names and runs :t on each. For simplicity, I'll split on spaces; but if you want to ask for the types of many expressions rather than just names you may want to do some more complicated delimiting/parsing. So, in ~/.ghci, add a line like this:
:def manyt (\s -> Prelude.return (Prelude.unlines [":t " Prelude.++ n | n <- Prelude.words s]))
(The excessive Prelude qualification is so that this works even when -XNoImplicitPrelude is enabled.) Try it out:
> :manyt length take drop splitAt (!!) replicate
length :: Foldable t => t a -> Int
take :: Int -> [a] -> [a]
drop :: Int -> [a] -> [a]
splitAt :: Int -> [a] -> ([a], [a])
(!!) :: [a] -> Int -> a
replicate :: Int -> a -> [a]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With