5 ms·
FizzBuzz in Haskell [pdf]
- dottedmag 4y agoUgh. There are languages that encourage this kind of time wasting. Avoid them. Also, > Exhibit A, in some cases, performs the ‘mod‘ 3 and ‘mod‘ 5 tests more than once Isn't Haskell a language with controlled side-effects? Compiler ought to see that the same operation is performed twice and factor it out. There are no mutable variables in the scope.
- mrkeen 4y ago> Compiler ought to see that the same operation is performed twice and factor it out. Yes but the trouble is when Haskell does optimisations, it's criticised for having unpredictable performance characteristics.
- yakubin 4y agoSee also the fastest FizzBuzz written in x86-64+AVX2 Assemby, running at 45GiB/s: <https://codegolf.stackexchange.com/questions/215216/high-throughput-fizz-buzz/ https://codegolf.stackexchange.com/questions/215216/high-thr...> <https://github.com/orent/htfizzbuzz/blob/master/fizzbuzz.S https://github.com/orent/htfizzbuzz/blob/master/fizzbuzz.S>
- metadat 4y agoDiscussed one year ago: https://news.ycombinator.com/item?id=29031488 https://news.ycombinator.com/item?id=29031488 (1260 points, 256 comments) p.s. It's actually 55GiB/s, aka 59GB/s :)
- Qem 4y agoHow does the Haskell version fare?
- quickthrower2 4y agoThis one gets you 0.2 job offers / day.
- deleted 4y ago[deleted]
- HyperSane 4y agoThat is one of the greatest feats of programming I've ever seen.
- adamjc 4y agoI know this is a "toy" paper, but is enough to reason as to why few use Haskell in production (ignoring the tooling issue). It's pretty gnarly even for a basic problem. > Functional programmers! Remember higher-order functions! I didn't realise utilising HoF meant rolling your own interpreter ;). Pure FP is very interesting, but having to write entirely without side-effects would be a real challenge for me. I use js+ramda[0], we effectively write our services as a `pipe`, you just stick in transforming fns whereever you need to, e.g.: const handler = pipe( fn1, fn2, fn3 )(event) The underlying implementation of `fn1` for example doesn't need to be written in an fp paradigm, it can be written imperatively. You could then slowly-slowly chop out the imperative functions for fp ones if you really wanted, but IMHO some problems are best solved imperatively. My advice is instead: "Programmers! Remember to use the right tool for the job!" [0] https://ramdajs.com/docs/ https://ramdajs.com/docs/
- cmeacham98 4y agoThis paper takes a deliberately weird solution to the problem. FizzBuzz can be done more "normally" in Haskell with similar levels of simplicity as non-FP languages: https://wiki.haskell.org/Fizzbuzz https://wiki.haskell.org/Fizzbuzz
- spacemadness 4y agoThis looks very clear and straightforward even knowing nothing about Haskell syntax.
- jrumbut 4y agoI found this to be well above average as an intro to Haskell.
- kasajian 4y agoplus you don't even need the first line.
- antonvs 4y agoThe paper even gives three normal examples early on, including one similar to the one in your link. People who miss that are clearly looking hard for some reason to dismiss the work.
- ohbtvz 4y agoSo, instead of checking twice if the number is divisible by five, you check once but then you check the type of every instruction again. How is that a win?
- quickthrower2 4y agoYeah it is a bit of a strawman, especially as Exhibit B solves this problem (and an inline function would remove the duplication). However as the mention at the end this isn't a serious performance tuning session, or even a serious answer to FizzBuzz, but a way to introduce some Haskell concepts.
- tromp 4y agoHere's the resulting Haskell code fizzbuzz n = (test 3 "fizz" . test 5 "buzz") id (show n) where test d s x = if n `mod` d == 0 then const (s ++ x "") else x main = mapM_ (putStrLn . fizzbuzz) [0..100] you can compile with ghc and run...
- bradrn 4y agoA minor comment, but I’d reformat this a bit to make its structure clearer: fizzbuzz n = go id (show n) where go = test 3 "fizz" . test 5 "buzz" test d s x = if n `mod` d == 0 then const (s ++ x "") else x
- IAmYourDensity 4y agoExplanation: If both tests fail, return the 'id' (identity) function which when applied to (show n) will return the number as a string. If one or both tests succeed, return a 'const' function which will simply return the appropriate string and ignore the (show n). This example was illuminating. It helped me realize I'm wasting too much time with Haskell.
- LelouBil 4y agoI really liked this ! As a novice ( but really interested) in functional programming, I had a bit of troubles with Haskell and eventually didn't find something I could do to toy with it. This paper is basically what I want to try doing in functional programming : almost write maths, but that does the computation. It wasn't too complex either ! Can someone point me to other resources in the same genre ?
- Jtsummers 4y agoThe Haskell Road to Maths, Logic and Programming by Kees Doats and Jan van Eijck may be of interest to you. https://staff.fnwi.uva.nl/d.j.n.vaneijck2/HR/ https://staff.fnwi.uva.nl/d.j.n.vaneijck2/HR/
- frozenlettuce 4y agoFor the for math-y and proof oriented content, you can look at "The Tao of Functional Programming" For something more practical, but in PureScript, this e-book has tons of examples and detailed explanations (most concepts translate to Haskell) https://jordanmartinez.github.io/purescript-jordans-reference-site/content/01-Getting-Started/index.html https://jordanmartinez.github.io/purescript-jordans-referenc...
- Terretta 4y agoA few times FizzBuzz appeared on HN, I made this point ... > "Outputting fizzbuzz should be done by executing the (one and only) piece of code that outputs fizz, followed by the (one and only) piece that outputs buzz. That is because fizzing and buzzing are two separate activities – consider the FizzBuzzHissHowl problem, where hiss and howl are printed for multiples of 7 and 11 respectively. The program design of Exhibit A and B would lead to an explosion of code complexity." ... but always got voted off the island in favor of Exhibit A. Unclear to me why the canonically correct answer examples for various languages are almost universally mistaken as described there, and yet so fiercely defended! Perhaps Exhibit C is like Fight Club, to be kept hush hush as a shibboleth for sussing out Principal Engineers?
- naet 4y agoI think a major part of the FizzBuzz test is writing something simple and understandable. Premature/unnecessary optimization isn't a plus, it's a minus. Start with the basic "Exhibit A" for a FizzBuzz. If a second problem asks you to write something for "FizzBuzzHissHowl", then you can start introducing more code complexity.
- kazinator 4y agoThis person can say, "I worked at FB using Haskell".
- Tao3300 4y ago> I don't know about Java. It's very solvable in Java, as we see here: https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpris...