haskellmonadshaskell-platform

print regards following functions as arguments


So I'm learning Haskell and have a broader programm which i want to run, but I managed to narrow down why it doesn't work to this problem:

putSentence:: String -> IO ()
putSentence sentence = print sentence

func 0 =  putSentence "Sample Text."
func x = if 3 == x then putSentence "Three." func (x-1) else putSentence "Not three." func (x-1)

The above code doesn't compile because putSentence takes the following func (x-1) as an additional argument. This is the first time Haskell did it like that for me, and I already tried shuffling the priority around with parentheses and $, but didn't find a way to fix it, so help would be appreciated.


Solution

  • The expression

    putSentence "Three." func (x-1)
    

    calls putSentence with three arguments: "Three.", func, and (x-1). This is wrong.

    What you probably want to do is to perform the IO actions one after another. For that you can use >>:

    putSentence "Three." >> func (x-1)
    

    Alternatively, use a do block:

    do putSentence "Three."
       func (x-1)
    

    E.g.

    func x = if 3 == x
       then do
          putSentence "Three."
          func (x-1)
       else do
          putSentence "Not three."
          func (x-1)