andymac-2 / haskell-docs

MIT License
0 stars 3 forks source link

Added FizzBuzz Haskell implementation #2

Closed kahlil29 closed 6 years ago

kahlil29 commented 6 years ago

Reference : https://www.geeksforgeeks.org/fizz-buzz-implementation/

andymac-2 commented 6 years ago

Thank you very much!

I do have a couple of pointers however for writing code in a more Haskell-ey way however:

  1. The keyword do is unnecessary because fmap replaceNumbers [1..100] is a single line of code. do is also syntactic sugar for some very complicated monadic operations, so unless you need the monad part of it, It probably complicates things a bit.
  2. if, then, and else are generally not very nice looking in Haskell, especially when nested, so are generally avoided. Guards can be used to reduce the if/then/else pyramids: for example, the following two functions are the same:
sign' :: Int -> Int
sign' x = 
    if x > 0
        then 1
        else if x < 0
            then -1
            else 0

sign :: Int -> Int
sign x
    | x > 0 = 1
    | x < 0 = -1
    | otherwise = 0

Other than that, your code is very understandable, and generic solutions are always appreciated in Haskell. Thank you again!

kahlil29 commented 6 years ago

Thanks for your feedback! I'm just used to using the do notation a lot so I guess that's why it came in. Will replace it with a let .. in if there are multiple line functions in later code.

I had initially used a case match for the if/else part, but yes, guards seem to be a great feature for this use-case.

The changes are pushed 😄

andymac-2 commented 6 years ago

Perfect!