4

I'm learning Haskell, and these are my first steps in trying to understand the syntax. I'm having a very hard time, and the language reference is not helpful, unfortunately.

I tried to do :type (line_length 1 2 3 4) and I have red the reference on printf However, it uses symbols with vague meaning instead of words. So, I'm stuck and asking for help.

Put simple: what do I write in place of ???.

line_length :: Integer -> Integer -> Integer -> Integer -> ???
line_length ax ay bx by =
            printf ("The length of the line between the points" ++
            "(%d,%d) and (%d,%d) is %.5f\n") ax ay bx by
            ((((fromIntegral (ax - bx)) ** 2.0) +
            ((fromIntegral (ay - by))) ** 2.0) ** 0.5)
8
  • 7
    Printf is not a good start if you want to learn Haskell. What type would you like line_length to return?
    – augustss
    Feb 22, 2012 at 10:37
  • 2
    BTW, (^ 2) is better than (** 2), and sqrt is better than (** 0.5).
    – augustss
    Feb 22, 2012 at 10:44
  • 1
    The language reference certainly isn't a very good learning source for trying to understand the syntax. Have you not tried one of the easy books/tutorials, such as LYAH, RWH or YAHT? Feb 22, 2012 at 10:47
  • 1
    I would also like to quote from the documentation that you link to: "The return value is either String or (IO a)."
    – augustss
    Feb 22, 2012 at 10:51
  • 2
    "I actually don't need to return anything from line_length"?? A function that doesn't need to return anything is completely meaningless in Haskell. Feb 22, 2012 at 13:28

2 Answers 2

12

The overloaded return type of printf :: PrintfType r => String -> r can be a little confusing, but it helps to look at the PrintfType instances which are available:

  1. First, we have this instance:

    instance (PrintfArg a, PrintfType r) => PrintfType (a -> r)
    

    This one is used to allow printf to take a variable number of arguments. This makes sense due to currying, but you probably won't think of it as the "true" return type of the function. For more information about how this works, see How does printf work in Haskell?.

  2. Then we have this instance,

    instance PrintfType (IO a)
    

    This means that we can use it as an IO action. Similar to print, this will print the result to standard output. The result of the action is undefined, so you should just ignore it.1

    > printf "Foo %d\n" 42 :: IO ()
    Foo 42
    > it
    *** Exception: Prelude.undefined
    
  3. And finally,

    instance IsChar c => PrintfType [c]
    

    The type class here is mostly to allow this to be Haskell 98 compliant. Since the only instance of IsChar is Char, you can think of this as

    instance PrintfType String
    

    but that would require the GHC-specific extension FlexibleInstances.

    What this instance means is that you can also just return a String without printing it, similar to sprintf in C.

    > printf "Foo %d\n" 42 :: String
    "Foo 42\n"
    

So depending on whether you want to print the result or just return it, you can replace ??? with either IO () or String in your example.

However, there is another problem with your code in that the compiler is unable to determine which type you wanted the last argument to be, so you have to help it out with a type annotation:

line_length :: Integer -> Integer -> Integer -> Integer -> String
line_length ax ay bx by =
            printf ("The length of the line between the points" ++
            "(%d,%d) and (%d,%d) is %.5f\n") ax ay bx by
            ((((fromIntegral (ax - bx)) ** 2.0) +
            ((fromIntegral (ay - by))) ** 2.0) ** 0.5 :: Double)

1 This is done to avoid extensions. With extensions, we could write instance PrintfType (IO ()) and the result would be ().

0
1

When I get stuck with type signatures, I let Haskell do the hard work for me. That is, I

  • write the definition without a type signature
  • load it into ghci
  • interactively check the type that Haskell gave it
  • copy that signature down (making sure it's what I intended) in the source file
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.