Skip to content

Instantly share code, notes, and snippets.

@AphonicChaos
Last active March 14, 2016 16:16
Show Gist options
  • Save AphonicChaos/869f6f987c72de4208fa to your computer and use it in GitHub Desktop.
Save AphonicChaos/869f6f987c72de4208fa to your computer and use it in GitHub Desktop.
What happens when I try to make Haskell look like F#
module FSharp where
import Text.Printf
import Prelude hiding ((.))
(%) :: Integral a => a -> a -> a
(%) = mod
(.) :: a -> (a -> b) -> b
x . f = f x
(|>) :: a -> (a -> b) -> b
(|>) = (.)
toString :: () -> Int -> String
toString () = show
printfn :: PrintfType r => String -> r
printfn = printf
module List
( iter
, L.map
) where
import qualified Data.List as L
import qualified Control.Monad as M
import Prelude hiding ((.))
import Data.Foldable ()
iter :: (Foldable t, M.Monad m) => (a -> m b) -> t a -> m ()
iter = M.mapM_
let main =
[1..100]
|> List.map (fun x ->
match x with
| _ when x % 15 = 0 ->"fizzbuzz"
| _ when x % 5 = 0 -> "buzz"
| _ when x % 3 = 0 -> "fizz"
| _ -> x.ToString())
|> List.iter (fun x -> printfn "%s" x)
module Main where
import qualified List
import FSharp
import Prelude hiding ((.))
main =
[1..100]
|> List.map (\x ->
case () of
_ | x % 15 == 0 -> "fizzbuzz"
_ | x % 5 == 0 -> "buzz"
_ | x % 3 == 0 -> "fizz"
_ -> x.toString())
|> List.iter (\x -> printfn "%s" x)
@AphonicChaos
Copy link
Author

Kids, don't do this at home. A lot of the code in the Haskell variant is redundant and uneccessary, but I wanted to be as true to the original FSharp variant. If I were doing this in idiomatic Haskell, I might just pass putStrLn to Control.Monad.forM_.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment