Skip to content

Instantly share code, notes, and snippets.

View cacilhas's full-sized avatar
💭
I may be slow to respond.

Montegasppα ℭacilhας cacilhas

💭
I may be slow to respond.
View GitHub Profile
@cacilhas
cacilhas / main.moon
Created February 4, 2024 20:51
TIC-80 time function test
export ^
local start, ticks
show = (y) =>
w = print @, 240, 0, 0, true
print @, (240-w)/2, y, 12, true
BOOT = ->
@cacilhas
cacilhas / main.moon
Last active February 4, 2024 19:43
TIC-80 states :: second suggestion
export ^
local *
local state
----------------------------------------------------------
game =
player:
x: 120
y: 68
@cacilhas
cacilhas / main.moon
Created February 4, 2024 19:11
TIC-80 states :: first suggestion
export ^
local *
bg = 12
state = "game"
p =
x: 120
y: 68
m_ind = 0
s_ind = 0
@cacilhas
cacilhas / fact.pl
Last active November 10, 2023 21:16
Factorial in SWI Prolog using CLP(FD)
:- module(fact, [fact/2]).
:- [library(clpfd)].
fact(N, F) :- N #> 0,
N1 #= N - 1,
F #= N * F1,
fact(N1, F1).
fact(0, 1).
@cacilhas
cacilhas / fact.pl
Last active November 10, 2023 21:17
Factorial in SWI Prolog postponing F calculation
:- module(fact, [fact/2]).
fact(N, F) :- integer(N),
N > 0,
succ(N1, N),
freeze(F1, F is N * F1),
fact(N1, F1).
fact(0, 1).
@cacilhas
cacilhas / fact.pl
Last active November 10, 2023 21:17
Factorial in SWI Prolog using D.C.G.
:- module(fact, [fact/2]).
fact(N, F) :- integer(N), fact(N, 1, F).
fact(N) :- { N > 0 },
{ succ(N1, N) },
prod(N),
fact(N1).
fact(0, A, A).
@cacilhas
cacilhas / fact.pl
Last active November 10, 2023 21:17
Factorial in SWI Prolog using auxiliary predicate
:- module(fact, [fact/2]).
fact(N, F) :- integer(N), fact(N, 1, F).
fact(N, A, F) :- N > 0,
succ(N1, N),
A1 is N * F,
fact(N1, A1, F).
fact(0, A, A).
@cacilhas
cacilhas / fact.sc
Created October 31, 2023 22:03
Factorial aspect
import scala.annotation.tailrec
object Fact:
extension(n: Natural)
def fact: BigInt = _fact(1, n)
@tailrec
private def _fact(acc: BigInt, n: Natural): BigInt =
n match
case Natural(0) => acc
@cacilhas
cacilhas / natural.sc
Created October 31, 2023 21:55
Natural numbers
final case class Natural private(value: Int) extends AnyVal
object Natural:
given Conversion[Int, Natural] with
def apply(i: Int): Natural =
if (i >= 0) Natural(i)
else throw new IllegalArgumentException(f"invalid negative number ${i}")
given Conversion[Natural, Int] = (n: Natural) => n.value
@cacilhas
cacilhas / natural.sc
Created October 31, 2023 21:36
Natural Numbers canonical implementation
sealed trait Natural
object Natural:
given Conversion[Int, Natural] with
def apply(i: Int): Natural =
i match
case 0 => Zero
case n if n > 0 => Succ(n - 1)
case n if n < 0 => throw new IllegalArgumentException(f"invalid negative number ${n}")