Jump to content

haskell type

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
arpho

arpho

    Newbie

  • Members
  • Pip
  • 1 posts
Hi, I am new to haskell :confused:, can somebody help me with this error?


Couldn't match expected type `(t -> t) -> IO ()'

           against inferred type `IO ()'

    In the expression: doAverage (n + 1) (media 0 0)

    In the expression:

        if sample /= 0 then

            doAverage (n + 1) (media 0 0)

        else

            putStrLn "ciao"

    In the expression:

        do s <- getLine

           let sample = read s

           if sample /= 0 then

               doAverage (n + 1) (media 0 0)

           else

               putStrLn "ciao"

Couldn't match expected type `(t -> t) -> IO ()'

           against inferred type `IO ()'

    In the expression: doAverage (n + 1) (media 0 0)

    In the expression:

        if sample /= 0 then

            doAverage (n + 1) (media 0 0)

        else

            putStrLn "ciao"

    In the expression:

        do s <- getLine

           let sample = read s

           if sample /= 0 then

               doAverage (n + 1) (media 0 0)

           else

               putStrLn "ciao"


here is my code


module Main

    where


import IO

--import Random

--media :: forall t. (Fractional t) => t -> (t, t) -> (t, t)

media s a n= (a*n+s)/(n+1)

doAverage ::(Num t =>(t,t)-> IO ())



main =do

	hSetBuffering stdin LineBuffering

--	num <- randomRIO (1::Int, 100)

	putStrLn "calcolo iterativo della media"

	

	doAverage (1,0) --sample numero campioni e media attuale


doAverage  (n,aver) =do

	s<-getLine

	let sample = read s

	--putStrLn ("il valore medio dopo il "++(show (n+1)++"campione è"++(show

(media sample (aver,n))))

	if sample/=0 

		then doAverage (n+1) (media 0  0) --sample aver n)

		else putStrLn "ciao"

thanks in advance

#2
asafe

asafe

    Programmer

  • Members
  • PipPipPipPip
  • 107 posts
I think that's because if and the else don't return the same type. And does not media take three args?
My Italian is awful : are you trying to calculate the average?

#3
asafe

asafe

    Programmer

  • Members
  • PipPipPipPip
  • 107 posts
"""

Edited by asafe, 03 August 2010 - 11:10 AM.
dup


#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Your problem is that you're attempting to pass arguments into doAverage that doAverage is not expecting. Look at your type signature for doAverage:
doAverage :: Num t => (t, t) -> IO ()
This means that doAverage takes one argument, a pair of t, and returns an empty IO monad. However, in your if/else statement, you call doAverage with two arguments:
if sample /= 0
    then doAverage (n + 1) (media 0 0)
Namely, you've tried to use doAverage as if it had the type signature
doAverage :: (Num t, Num u) => t -> (u -> u) -> IO ()
It does not, so you should run doAverage with a pair of t, as I expect is what you had intended:
if sample /= 0
    then doAverage (n + 1, media 0 0 n)
And that will solve that problem, but after that you will get another build error:
    Could not deduce (Fractional t) from the context (Num t)
      arising from a use of `media' at Main.hs:23:25-35
    Possible fix:
      add (Fractional t) to the context of
        the type signature for `doAverage'
    In the expression: media 0 0 n
    In the first argument of `doAverage', namely `(n + 1, media 0 0 n)'
    In the expression:
        if sample /= 0 then
            doAverage (n + 1, media 0 0 n)
        else
            putStrLn "ciao"
I'll give you a crack at finding out why. :)
Wow I changed my sig!

#5
asafe

asafe

    Programmer

  • Members
  • PipPipPipPip
  • 107 posts
/ does fractional division.
I want a crack.