Skip to content

Instantly share code, notes, and snippets.

View callerobertsson's full-sized avatar
Converting coffee to code

Calle Robertsson callerobertsson

Converting coffee to code
View GitHub Profile
@callerobertsson
callerobertsson / hanoi.hs
Created February 22, 2015 00:42
Haskell: Tower of Hanoi
type Peg = String
type Move = (Peg, Peg)
hanoi :: Integer -> Peg -> Peg -> Peg -> [Move]
hanoi 0 _ _ _ = []
hanoi n a b c = hanoi (n - 1) a c b ++ [(a, c)] ++ hanoi (n - 1) b a c
main = do
print $ hanoi 1 "a" "b" "c"
print $ hanoi 2 "a" "b" "c"
@callerobertsson
callerobertsson / LucasCarmichaelNumbers.hs
Created January 21, 2015 13:34
Haskell: Lucas Carmichael number generator
-- Lucas Carmichael Numbers
--
-- inspired by the Youtube channel Numberphile
-- episode https://www.youtube.com/watch?v=yfr3BIk6KFc
-- "Something special about 399 (and 2015)"
--
-- by calle@upset.se, jan 2015
import Data.Numbers.Primes
@callerobertsson
callerobertsson / cans.hs
Last active August 29, 2015 14:13
Haskell: Calculates number of cans in a pyramid or sum of all cans in pyramids
-- cans.hs
-- Calculate number of cans in one pyramid
cansInPyramid :: Int -> Int
cansInPyramid 1 = 1
cansInPyramid n = n + cansInPyramid (n - 1)
-- Smaller solution
cansInPyramid' n = sum [1..n]
@callerobertsson
callerobertsson / page-template.html
Created May 14, 2013 06:51
HTML: Page template #snippet
<!DOCTYPE html>
<!-- by calle@stenhall.com -->
<html>
<head>
<title>TITLE</title>
<style>
@import url("reset.css");
@import url("style.css");
</style>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
@callerobertsson
callerobertsson / filterFile.js
Created May 13, 2013 15:53
Javascript (NodeJS): A simple script for extracting parts in a textfile between two regex matching lines. #snippet
// Filter File
// a simple script for extracting parts in a textfile between two lines.
// by Calle Robertsson, calle@upset.se, 2013.
var fs = require('fs');
var filename = process.argv[2];
if (!filename) {
throw new Error('Missing command line argument for file name.');
@callerobertsson
callerobertsson / compareObjects.js
Created April 28, 2013 22:18
Javascript: Object comparision with ignored fields. Original source can be found at http://www.sencha.com/forum/showthread.php?59240-Compare-javascript-objects but without ignored fields. #snippet
// The javascript compare objects function
function compareObjects(o1, o2, ignoreFields){
ignoreFields = ignoreFields || [];
//console.log('Ignore: ' + JSON.stringify(ignoreFields));
for(var p in o1) {
if (ignoreFields.indexOf(p) < 0) {
if(o1[p] !== o2[p]){
return false;
}
}