This file contains 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
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())) |
This file contains 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 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 |
This file contains 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
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) |
This file contains 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
#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))) |
This file contains 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
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 |
This file contains 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
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: |
This file contains 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 StringIO | |
def get_big_numbers(): | |
number = """ | |
37107287533902102798797998220837590246510135740250 | |
46376937677490009712648124896970078050417018260538 | |
74324986199524741059474233309513058123726617309629 | |
91942213363574161572522430563301811072406154908250 | |
23067588207539346171171980310421047513778063246676 | |
89261670696623633820136378418383684178734361726757 |
This file contains 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
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): |
This file contains 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
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))); | |
} |
This file contains 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 split([]) = ([],[]) | |
| split(x::[]) = ([x],[]) | |
| split(x::y::xs) = | |
let | |
val (L,R) = split(xs) | |
in | |
(x::L,y::R) | |
end | |
fun merge([],[]) = [] |