Skip to content

Instantly share code, notes, and snippets.

View coproduto's full-sized avatar

Cast coproduto

View GitHub Profile
f = x ^ 2
module Lib
( totalFuelRequirements
) where
import System.IO (isEOF)
-- gets the fuel a given mass would need if fuel had 0 mass
naiveFuelRequirement :: Integer -> Integer
naiveFuelRequirement mass = max 0 ((mass `div` 3) - 2)
module Main where
import Data.Array (Array, (//), listArray, (!))
import Data.Maybe (fromJust)
import Control.Monad.State (State, state, get, execState)
import Control.DeepSeq (deepseq)
import System.IO (getContents)
type Program = Array Int Int
type Args = Maybe (Int, Int, Int)
{-# LANGUAGE MultiParamTypeClasses #-}
import Control.Monad (join)
newtype Compose f g a =
Compose { getCompose :: f (g a) } deriving Show
instance (Functor f, Functor g) => Functor (Compose f g) where
fmap f (Compose x) = Compose $ (fmap . fmap) f x
import Data.Sequence (Seq(..), (|>), singleton)
data Tree a = Node (Tree a) a (Tree a) | Leaf
preOrder :: Applicative f => (a -> f b) -> Tree a -> f (Tree b)
preOrder _ Leaf = pure Leaf
preOrder f (Node left x right) =
(\x l r -> Node l x r) <$> (f x) <*> (preOrder f left) <*> (preOrder f right)
postOrder :: Applicative f => (a -> f b) -> Tree a -> f (Tree b)
import Data.Char (isLetter, isNumber, isSpace)
import Data.List (span, dropWhile, lookup)
type ParseError = String
-- A parser is a function that takes a string and returns a parse error or a
-- pair of the parsed data and the remaining string.
type Parser a = String -> Either ParseError (a, String)
type Context = [(String, Term)]
data Point { x :: Float, y :: Float }
data Circle { radius :: Float, center :: Point }
data Rect { topLeft :: Point, bottomRight :: Point }
class Shape s where
area :: s -> Float
drawable :: s -> VectorDrawable -- não defino o tipo aqui
instance Shape Circle where
area c = 2 * pi * (radius c)
// definição de uma das aplicações mais básicas de sum types: o tipo opcional, normalmente
// chamado de Optional (em Swift), Option (em Rust) ou Maybe (em Haskell)
// o jeito mais simples de simular um sum type em JS é simplesmente ter uma tag no tipo.
// você pode facilitar isso usando "construtores" apropriados.
const Some = (x) => { kind: "some", value: x } // equivalente à função inj1
const None = () => { kind: "none" } // equivalente à função inj2
// o caso geral de pattern matching pode ser definido com uma função que executa o valor
// de acordo com o tipo.
delta :: Floating t => (t -> t) -> t -> t -> t -> t
delta sqrt a b c = sqrt(b ** 2 - 4 * a * c)
solutions :: Fractional t => t -> t -> t -> (t, t)
solutions a b delta = ((-b + delta) / (2 * a)), (-b - delta) / (2 * a))
bhaskara :: (Floating t, Fractional t) => (t -> t -> t -> t) -> (t -> t -> t -> (t, t)) -> (t, t)
bhaskara delta sols a b c = sols a b (delta a b c)
main :: IO ()
data QuadraticEq =
Quadratic Double Double Double
delta :: (Double -> Double) -> QuadraticEq -> Double
delta sqrt (Quadratic a b c) = sqrt(b ** 2 - 4 * a * c)
quadraticRoots :: (QuadraticEq -> Double) -> QuadraticEq -> (Double, Double)
quadraticRoots delta q@(Quadratic a b _) = (solution1, solution2)
where solution1 = solve (+) a b
solution2 = solve (-) a b