Skip to content

Instantly share code, notes, and snippets.

View Koitaro's full-sized avatar

Yasusi Koibuti Koitaro

View GitHub Profile
proc addcomma {num} {
set s {}; set r {}
if {$num < 0} {
set result "-"
set num [expr abs($num)]
}
lassign [split $num .] upper lower
set len [string length $upper]
for {set i 0} {$i < $len} {incr i} {
set s [list [string index $upper $i] $s]
namespace path {::tcl::mathop ::tcl::mathfunc}
proc primes n {
set half [/ [- $n 1] 2]
set xs [lrepeat $half 1]
set p 0; set root [/ [sqrt $n] 2]
while {$p < $root} {
if {[lindex $xs $p]} {
set step [+ [* 2 $p] 3]
set index [+ $p $step]
while {$index < $half} { lset xs $index 0; incr index $step }
proc prime? n {
if {$n <= 0} { return 0 }
if {$n >= 341550071728321} { error prime? }
if {$n < 4759123141} { set as {2 7 61} } else { set as {2 3 5 7 11 13 17} }
if {$n == 2 || $n == 3 || $n == 5 || $n == 7 || $n == 11 || $n == 13 || $n == 17 || $n == 61} {
return 1
}
if {$n == 1 || $n % 2 == 0} { return 0 }
set d [expr {$n - 1}]
while {$d % 2 == 0} { set d [expr {$d / 2}] }
# Zeller's congruence
# 0 => Sunday
proc zeller {y m d} {
set t {0 3 2 5 0 3 5 1 4 6 2 4}
if {$m < 3} { incr y -1 }
expr {($y + $y/4 - $y/100 + $y/400 + [lindex $t $m-1] + $d) % 7}
}
/* Concurrent Clean code for ini file */
:: INI = {section :: String, key :: String, value :: String}
instance toString INI where
toString ini = ini.section +++ "\t" +++ ini.key +++ "\t" +++ ini.value
toStringINIList :: [INI] -> String
toStringINIList ini = f "" "" ini where
f str _ [] = str
definition module Primes
isPrime :: Int -> Bool
isPrimeMR :: Int -> Bool
primes :: [Int]
primeArray :: Int -> {#Bool}
primeList :: Int -> [Int]
rangePrimes :: Int Int -> [Int]
@Koitaro
Koitaro / Clean code for sets
Created April 22, 2010 11:29
permutation, perm, powerSet
permutation :: [a] -> [[a]] | Eq a
permutation [] = [[]]
permutation xs = [[x:rest] \\ x <- xs, rest <- (permutation o filter ((<>) x)) xs]
perm :: [[a]] -> [[a]] | Eq a
perm [] = [[]]
perm [xs:xss] = [[x:rest] \\ x <- xs, rest <- (perm o map (filter ((<>) x))) xss]
powerSet :: [a] -> [[a]]
powerSet [] = [[]]
@Koitaro
Koitaro / PairingHeap.dcl
Created July 10, 2010 14:52
Pairing Heap for Clean Language
definition module PairingHeap
:: Heap a = Empty | Node a [Heap a]
peek :: (Heap a) -> a
push :: a -> (Heap a) -> (Heap a) | Ord a
pop :: (Heap a) -> (Heap a) | Ord a
listToHeap :: ([a] -> (Heap a)) | Ord a
heapToList :: (Heap a) -> [a] | Ord a
@Koitaro
Koitaro / gist:470801
Created July 10, 2010 15:55
Library for Project Euler
module Main
import StdEnv, StdDebug, StdLib, BigInt, Rational//, StdOverloadedList, Parsers
//---------------------------------------------------------------------------------
S :: (a -> b -> c) (a -> b) a -> c
S f g x = f x (g x)
//---------------------------------------------------------------------------------
@Koitaro
Koitaro / EulerIO.dcl
Created July 12, 2010 21:55
I/O library for Project Euler
definition module EulerIO
put :: a *World -> *World | <<< a
puts :: [a] *World -> *World | <<< a
reads :: File -> [Char]
readlines :: File -> [String]
splitCharsAt :: Char -> ([Char] -> [[Char]])
splitStringAt :: Char -> String -> [String]