Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers
by tikhonj on Thursday January 05, 2012 @05:57PM
(#3430993)
A couple of suggestions: functions like rem look better infix:
x `rem` 3
this means the same thing but is more readable (I think).Instead of folding, just map putStrLn over the list:
mapM_ (putStrLn . show') [1..100]
mapM_ is just a version of map for monads that ignores its result (it returns m () instead of m a). You can also use forM_ which is mapM_ with its arguments reversed.Finally, I think this site lets you format code by putting four spaces in front of each line. I hope :)
Edit: One other thing. You don't need all those parentheses in the first line.
rem x 3 == 0 && rem x 5 == 0 = "FizzBuzz"
x `rem` 3 == 0 && x `rem` 5 == 0 = "FizzBuzz"
Both versions should work and I think they are easier to read. I could see (rem x 3 == 0) also being clear, but the outermost parentheses do not help at all.
Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers
by it on Thursday January 05, 2012 @07:13PM
(#3431267)
Here's a shorter version:
fizzbuzz x | x `mod` 3 == 0 && x `mod` 5 == 0 = "FizzBuzz"
| x `mod` 3 == 0 = "Fizz"
| x `mod` 5 == 0 = "Buzz"
| otherwise = show x
main = putStr $ unlines $ map fizzbuzz [1..100]
Re:Why 37signals Doesn't Hire Programmers Based on Brainteasers
by rayval on Thursday January 05, 2012 @07:08PM
(#3431246)
For us old-school types, here's the 60-second version in C:
int main(int argc, char ** argv)
{
int i;
for (i=0; i< 100; i++)
{
if( (i%3==0)&&(i%5==0)) printf("FizzBuzz");
else if(i%3==0) printf ("Fizz");
else if(i%5==0) printf ("Buzz");
else printf("%d",i);
printf("\n");
}
}
show' :: Int -> String show' x
|((rem x 3 == 0) && (rem x 5 == 0)) = "FizzBuzz"
|rem x 3 == 0 = "Fizz"
|rem x 5 == 0 = "Buzz"
|otherwise = show x
main :: IO ()
main = putStrLn $ foldl (\acc x -> acc ++"\n" ++ show' x) "" [1..100]