Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kakkun61
Created March 22, 2019 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kakkun61/bac10a156aa17b827e22cadd856ecb5f to your computer and use it in GitHub Desktop.
Save kakkun61/bac10a156aa17b827e22cadd856ecb5f to your computer and use it in GitHub Desktop.
C-like Syntax Alt-Haskell Plan
main :: IO ()
main = putStrLn "Hello, World!"
val main: IO<Unit> = putStrLn("Hello, World!")

main :: IO ()
main = do
  l <- getLine
  putStrLn l
val main: IO<Unit> = {
  l <- getLine()
  putStrLn(l)
}

main :: IO ()
main = do
  let dayInSec = 24 * 60 * 60 :: Int
  putStrLn $ "Day in sec: " <> show dayInSec
val main: IO<Unit> = {
  val dayInSec: Int = 24 * 60 * 60
  putStrLn("Day in sec: " <> show(dayInSec))
}

add :: Int -> Int -> Int
add x y = x + y
val add: Int -> Int -> Int = (x, y) => x + y
// or
fun add(x: Int, y: Int): Int = x + y

abs :: (Double, Double) -> Double
abs (x, y) = sqrt $ x ** 2 + y ** 2
val abs: (Double, Double) -> Double = ((x, y)) => sqrt(x ** 2 + y ** 2)
// or
fun abs(p: (Double, Double)): Double =
  case(p) {
    (x, y) -> sqrt(x ** 2 + y ** 2)
  }

import Data.Foldable
import Data.IORef

main :: IO ()
main = do
  accRef <- newIORef (0 :: Int)
  for_ [0 .. 9] $ \i ->
    modifyIORef accRef $ \acc -> acc + i
  s <- readIORef
  print s
val main: IO<Unit> = {
  var acc: Int = 0
  for (i in [0 .. 9])
    acc = acc + i
  print(acc)
}

data Maybe a
  = Just a
  | Nothing
  deriving (Show, Eq, Ord)
data Maybe<T> {
  Just(val: T)
  Nothing
  deriving (Show, Eq, Ord)
}

class Functor f where
  map :: (a -> b) -> f a -> f b
trait Functor<F<_>> {
  val map<T, S>: (T -> S) -> F<T> -> F<S>
  // or
  fun map<T, S>(f: T -> S, x: F<T>): F<S>
}

instance Functor Maybe where
  map f (Just a) = Just $ f a
  map _ Nothing = Nothing
instance Functor<Maybe<_>> {
  val map =
    (f, m) =>
      case(m) {
        Just(a) -> Just(f(a))
        Nothing -> Nothing
      }
  // or
  fun map(f, m) =
    case(m) {
      Just(a) -> Just(f(a))
      Nothing -> Nothing
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment