Skip to content

Instantly share code, notes, and snippets.

View edalorzo's full-sized avatar

Edwin Dalorzo edalorzo

View GitHub Profile
@edalorzo
edalorzo / Streams.sml
Last active April 19, 2020 19:17
Stream Implementation in SML
datatype 'a stream = Empty | Cons of 'a * (unit -> 'a stream)
fun from n = Cons(n, fn () => from(n +1))
fun takeWhile f xs =
case xs of
Empty => Empty
| Cons(h,t) => if not(f(h))
then Empty
else Cons(h, fn () => takeWhile f (t()))
@edalorzo
edalorzo / add-mult.sml
Created October 25, 2013 01:11
Add and Mult defined in terms of successor/predecessor functions
fun succ x = x + 1
fun pred x = x - 1
fun add x y =
if y = 0
then x
else add (succ x) (pred y)
fun mult x y =
if y = 0
@edalorzo
edalorzo / Euler17.sml
Last active December 26, 2015 02:09
Solution to the Problem Euler 17 in SML
exception InvalidInput
type Number = (int * int *int *int)
fun to_number n =
let
fun iter x =
if x = 0
then []
else (x mod 10) :: iter (x div 10)
@edalorzo
edalorzo / basic-lambda-calc.rkt
Created October 18, 2013 16:59
Some Basic Lambda Calculus Operations
#lang racket
(define identity (λ(x) x))
(define apply (λ(func) (λ(arg) (func arg))))
(define select_first (λ(first) (λ(second) first)))
(define select_second (λ(first) (λ(second) second)))
(define cond (λ(e1) (λ(e2) (λ(c) ((c e1) e2)))))
(define TRUE (λ(first) (λ(second) first)))
(define FALSE (λ(first) (λ(second) second)))
@edalorzo
edalorzo / keywords
Created October 8, 2013 00:02
SML Keywords
abstype and andalso as case do datatype else
eqtype end exception fn fun functor handle
if in include infix infixr let local nonfix
of op open orelse raise rec sharing sig
signature struct structure then type val
with withtype while
@edalorzo
edalorzo / PythonEurler9.py
Created May 9, 2013 14:15
My solution to Project Euler Problem # 9 with Python
def calc_triple(m, n):
a = n ** 2 - m ** 2
b = 2 * m * n
c = n ** 2 + m ** 2
return (a,b,c)
def find_triplet():
j = 1
sum = 0
while True:
@edalorzo
edalorzo / PythonEuler13.py
Created May 9, 2013 14:10
My solution to Project Euler Problem # 13 with Python
import StringIO
def get_big_numbers():
number = """
37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
89261670696623633820136378418383684178734361726757
@edalorzo
edalorzo / PythonEuler12.py
Created May 9, 2013 14:09
My solution to Project Euler Problem # 12 with Python
from itertools import groupby
from collections import defaultdict
def divides(k,n):
"""
Determines if number n is divisible by n
"""
return n % k == 0
def ldf(n, k):
@edalorzo
edalorzo / MemoizedFibonacci.java
Created May 9, 2013 04:55
Memoized Fibonacci Numbers with Java 8
static Map<Integer,Long> memo = new HashMap<>();
static {
memo.put(0,0L);
memo.put(1,1L);
}
public static long fibonacci(int x) {
return memo.computeIfAbsent(x, n -> Math.addExact(fibonacci(n-1), fibonacci(n-2)));
}
@edalorzo
edalorzo / merge-sort.sml
Last active December 16, 2015 19:09
Merge Sort Algorithm
fun split([]) = ([],[])
| split(x::[]) = ([x],[])
| split(x::y::xs) =
let
val (L,R) = split(xs)
in
(x::L,y::R)
end
fun merge([],[]) = []