This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
delta sqrt a b c = sqrt(b ** 2 - 4 * a * c) | |
solutions a b delta = ((-b + delta) / (2 * a)), (-b - delta) / (2 * a)) | |
bhaskara delta sols a b c = sols a b (delta a b c) | |
main = print (bhaskara (delta sqrt) solutions 3 (-7) 4) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bhaskara a b c = (s, s') | |
where s = ((-b) + (sqrt delta)) / (2 * a) | |
s' = ((-b) - (sqrt delta)) / (2 * a) | |
delta = b ** 2 / (4 * a * c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- lista infinita dos primos | |
primes :: [Integer] | |
primes = 2 : 3 : 5 : filter isPrime [7, 9 ..] | |
where isPrime n = null $ filter (divides n) (potentialDivisors n) | |
potentialDivisors n = takeWhile (<= isqrt n) primes | |
-- raiz quadrada inteira, aproximada para cima | |
isqrt :: Integer -> Integer | |
isqrt = ceiling . sqrt . fromIntegral |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- lista infinita dos primos | |
primes :: [Integer] | |
-- os números primos consistem de 2 seguido de todos os ímpares maiores que 2 que são primos | |
primes = 2 : filter isPrime [3, 5 ..] | |
-- um número é primo se a lista de divisores primos dele é vazia | |
where isPrime = null . primeDivisors | |
-- a lista de divisores primos é a lista de potenciais divisores dele que dividem ele | |
primeDivisors n = filter (divides n) (potentialDivisors n) | |
-- a lista de potenciais divisores de um número é a lista de primos menores ou iguais à raiz do número | |
-- aqui usamos takeWhile ao invés de filter pois queremos um prefixo finito de uma lista infinita |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- lista infinita dos primos | |
primes :: Integral a => [a] | |
primes = 2 : 3 : 5 : filter isPrime [7, 9 ..] | |
where isPrime n = null $ filter ((== 0) . (mod n)) (potentialDivisors n) | |
potentialDivisors n = takeWhile (<= isqrt n) primes | |
isqrt = ceiling . sqrt . fromIntegral |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Numeric | |
readHexFractional :: (Floating a, Eq a) => ReadS a | |
readHexFractional s = | |
let iReadHex = (readHex :: ReadS Integer) | |
in case iReadHex s of | |
((intPart, ('.' : fracPart)) : _) -> | |
let intPart' = fromIntegral intPart | |
(zeroPrefix, sigFrac) = break (/= '0') fracPart | |
in case iReadHex sigFrac of |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE FunctionalDependencies #-} | |
import Control.Monad.Trans.Class | |
-- A mônada livre de um functor ou é um valor simples (Pure) | |
-- ou é uma instância do functor aplicada recursivamente à mônada livre (Free) | |
data Free f a = Pure a | |
| Free (f (Free f a)) | |
-- se o tipo contido na mônada livre é um functor, então a mônada livre |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function escolherProduto(produto) { | |
// aqui você usa "produto" como quiser | |
} | |
// outra forma de definir, atribuindo uma função anônima a uma variável | |
const escolherProduto = (produto) => { | |
// aqui você usa produto como quiser | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T, R> T?.map(f: (T) -> R): R? = if (this == null) f(this) else null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- `Cont r a` é o tipo de uma computação que produz um `a` e, quando completa, | |
-- irá produzir um `r`. Você pode ver como uma linha de produção com uma parte | |
-- "faltando" - essa parte é a função `a -> r` que o `Cont` precisa para estar | |
-- "completo". | |
newtype Cont r a = Cont { runCont :: (a -> r) -> r } | |
-- pode não ser imediatamente óbvio por que uma função do tipo `(a -> r) -> r` | |
-- precisa, em geral, produzir um `a` internamente - por parametricidade, só existem | |
-- duas formas de uma função polimórfica do tipo `(a -> r) -> r` existir: |