Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

When was the last time you posted code

Name: Anonymous 2014-06-25 3:48

to /prog/?

For me, I think it was a week and a half ago. Would someone post a reasonable challenge or something?

Name: L. A. Calculus !jYCj6s4P.g 2014-06-25 10:35

>>11
REALLY? HOW MANY FUKIN LINES OF HASKALL WUD IT TAKE TO WRITE DA FOLLOWING PROGRAM? (STICK WITH DA PROCEDURAL PARADIGM, JUNIOR):
#include <stdio.h>

int main(void)
{
const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
size_t i;
int x;

x = 0;
for (i = 0; i < sizeof a / sizeof a[0]; i++)
if (++a[i] % 2 != 0) {
x += a[i];
printf(" %d", a[i]);
}
printf("\n%d\n", x);
return 0;
}

Name: Anonymous 2014-06-25 15:03

>>13
odds = filter odd . map succ $ [ 0 .. 9 ]
printNums = putStrLn . unwords . ("":) . map show
main = printNums odds >> print (sum odds)


That produces output identical to yours, and it's not even golfed. But if you insist on translating it from the C version as directly as possible, then (quelle surprise!) it will look pretty much like C:

import Control.Monad (when)
import Data.Array.IO
import Data.IORef

main = do
a <- newListArray (0, 9) [ 0 .. 9 ] :: IO (IOUArray Int Int)
i <- newIORef 0
x <- newIORef 0
cStyleFor (i $= 0, i $< 10, i += 1) $ do
a_i <- plusplus a =<< readIORef i
when (a_i `mod` 2 /= 0) $ do
x += a_i
putStr (' ' : show a_i)
x' <- readIORef x
putStrLn ('\n' : show x')

-- Helper functions so we can pretend we're banging rocks together instead
-- of using a real language
ref $= value = writeIORef ref value
ref $< value = (< value) `fmap` readIORef ref
ref += value = modifyIORef ref (+ value)

-- Has to be implemented separately from (+=) unless we want to whip out
-- some fucking typeclasses like the Illuminati
plusplus array index = do
old <- readArray array index
let new = old + 1
writeArray array index new
return new

cStyleFor (pre, cond, post) body = pre >> loop
where loop = cond >>= \test ->
if test then body >> post >> loop else return ()


Please excuse the mess, bitch.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List