Reputation: 11
lol :: IO int
lol = go -1 0 0 0
where go k m n p = case k of
-1 -> do{
q <- readLn ;
go 0 0 0 q
}
p -> do{
putStrLn $ show $ n
}
_ -> do{
q <- readLn ;
go (k+1) q (if q > m then n+q-m else n) p
return()
}
I(a haskell beginner) am doing some practise questions. I have written a recursive function but i don't know how to use it in main. Please help me to do solve this problem. This is my idea in C++ and I got it accepted.
#include<iostream>
using namespace std;
int main(){
long long int prev=0;
long long int count,ans=0;
cin >> count;
long long int p;
for(int i=0; i<count ;i++){
cin >> p;
if(p>prev){
ans+=p-prev;
}
prev=p;
}
cout << ans << endl;
}
Upvotes: 1
Views: 90
Reputation: 9767
How about this. I just translated it directly from C++
module Main where
go :: Int -> Int -> Int -> IO Int
go i prev acc
| i > 0 = do p <- readLn
if p > prev
then go (i-1) p (acc + (p - prev))
else go (i-1) p acc
| otherwise = return acc
main :: IO ()
main = do
count <- readLn :: IO Int
ans <- go count 0 0
print ans
Upvotes: 2
Reputation: 152682
There are several syntax errors:
where
(and all following lines) a little bitreturn ()
And one type error: you should write lol :: IO ()
. After fixing these, you may write main = lol
to use it (or simply name it main
instead of lol
). No idea whether it does what it's supposed to do, but that seems like a thing you should be able to test once you get it running.
Upvotes: 2