d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With Haskell > Help!
Add Reply New Topic New Poll
Member
Posts: 29,197
Joined: Feb 5 2007
Gold: 4,000.18
Jul 11 2012 05:46pm
So I've been learning Haskell on my own from a site I saw here, but I didn't get so far and I already have an assignment ><
This is one of the questions :
I need to do a real live average calculator (I.e put in a number and then add it to a list, show the average, prompt user to input another one, show the average, etc)

I did this:

Code
main = do
putStrLn "Hello, please enter a number and I will calculate the average: "

putStrLn "1."
input = getLine
a = input:[]
calculateAvrg a

putStrLn "2. "
input = getLine
b = input:[a]
calculateAvrg b

putStrLn "3. "
input = getLine
c = input:[b]
calculateAvrg c

putStrLn "4. "
input = getLine
d = input:[c]
calculateAvrg d

putStrLn "5. "
input = getLine
e = input:[d]
calculateAvrg e

putStrLn "6. "
input = getLine
f = input:[e]
calculateAvrg f

putStrLn "7. "
input = getLine
g = input:[f]
calculateAvrg g

putStrLn "8. "
input = getLine
h = input:[g]
calculateAvrg h

putStrLn "9. "
input = getLine
i = input:[h]
calculateAvrg i

putStrLn "10. "
input = getLine
j = input:[i]
calculateAvrg j

calculateAvrg :: (Fractional a) => [a] -> String
calculateAvrg x  
| (length x) <= 10 = putStrLn ("The average is " ++ (sum x)/(length x))
| otherwise = putStrLn("We are done!")



It won't run, the code is probably the most inefficient thing you've ever seen, but I just can't figure this language out.

If anyone is familiar and good with Haskell plz plz let me know I REALLY need help with this assignment...
Member
Posts: 30,717
Joined: Sep 19 2007
Gold: 254.00
Jul 11 2012 07:19pm
it's like you haven't even tried using google...
Member
Posts: 29,197
Joined: Feb 5 2007
Gold: 4,000.18
Jul 11 2012 07:32pm
Quote (J_B @ Jul 11 2012 08:19pm)
it's like you haven't even tried using google...


.... I have...

/e codes there are retarded... I need a simple (maybe inefficient) code that I can understand.

This post was edited by IsraeliSoldier on Jul 11 2012 07:38pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 12 2012 12:04am
Quote (IsraeliSoldier @ Jul 11 2012 09:32pm)
.... I have...

/e codes there are retarded... I need a simple (maybe inefficient) code that I can understand.


print welcome message
begin loop
print ask user for input
add input to array
print do_calc(array)
start loop over
Member
Posts: 299
Joined: Apr 11 2010
Gold: 2,491.00
Jul 12 2012 07:11am
Here is your code rewritten to run



calculateAvrg x
| len <= 10 = putStrLn ("The average is " ++ (show avg))
| otherwise = putStrLn ("We are done!")
where len = (length x)
avg = ((sum x) `div` (length x))

main = do
putStrLn "Hello, please enter a number and I will calculate the average: "

putStrLn "1."
input <- getLine
let i = (read input :: Int)
let a = i:[]
calculateAvrg a

putStrLn "2. "
input <- getLine
let i = (read input :: Int)
let b = i:a
calculateAvrg b

putStrLn "3. "
input <- getLine
let i = (read input :: Int)
let c = i:b
calculateAvrg c

putStrLn "4. "
input <- getLine
let i = (read input :: Int)
let d = i:c
calculateAvrg d

putStrLn "5. "
input <- getLine
let i = (read input :: Int)
let e = i:d
calculateAvrg e

putStrLn "6. "
input <- getLine
let i = (read input :: Int)
let f = i:e
calculateAvrg f

putStrLn "7. "
input <- getLine
let i = (read input :: Int)
let g = i:f
calculateAvrg g

putStrLn "8. "
input <- getLine
let i = (read input :: Int)
let h = i:g
calculateAvrg h

putStrLn "9. "
input <- getLine
let i = (read input :: Int)
let i' = i:h
calculateAvrg i'

putStrLn "10. "
input <- getLine
let i = (read input :: Int)
let j = i:i'
calculateAvrg j
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll