Skip to content

Instantly share code, notes, and snippets.

View shoooe's full-sized avatar
🏓

Thomas Pigarelli shoooe

🏓
View GitHub Profile
@shoooe
shoooe / caesar.hs
Created February 18, 2014 17:42
Caesar's chiper -- Haskell exercise from §5.5 of "Programming in Haskell"
import Data.Char
char2Int :: Char -> Int
char2Int l = ord l - ord 'a'
int2Char :: Int -> Char
int2Char i = chr (ord 'a' + i)
charAdvance :: Int -> Char -> Char
charAdvance i c | isLower c = int2Char (((char2Int c) + i) `mod` 26)
@shoooe
shoooe / plusrow.hs
Last active August 29, 2015 13:56
A simple program in Haskell written while reading "Programming in Haskell". It prints a diagonal line of +es.
makeString :: Char -> Int -> String
makeString _ 0 = []
makeString c i = c : (makeString c (i - 1))
putStringsLn :: [String] -> IO ()
putStringsLn [] = return ()
putStringsLn (s : ss) = do
putStrLn s
putStringsLn ss
@shoooe
shoooe / bubble_sort.hpp
Last active August 29, 2015 13:56
Implementation of bubble sort in C++ as an exercise.
#pragma once
#include <algorithm>
template<typename IT>
void bubble_sort(IT begin, IT end) {
while (true) {
bool c = false; // changed?
for (IT i = begin; i != end-1; i = i+1) {
if (*i > *(i+1)) {
@shoooe
shoooe / insertion_sort.hpp
Created February 25, 2014 03:46
Implementation of insertion sort as an exercise.
#pragma once
#include <algorithm>
template <typename IT>
void insertion_sort(IT begin, IT end) {
for (IT i = begin; i != end; ++i) {
for (IT j = i; j != begin; --j) {
if (*(j-1) > *j) std::iter_swap(j-1, j);
else break;
@shoooe
shoooe / merge_sort.hpp
Created February 25, 2014 03:47
Implementation of merge sort as an exercise.
#pragma once
#include <algorithm>
#include <vector>
namespace {
template <typename IT>
void inner_merge(IT begin, IT middle, IT end) {
using Container = std::vector<typename std::iterator_traits<IT>::value_type>;
@shoooe
shoooe / round_1a.hs
Created April 14, 2014 20:20
Solution to the Round 1A of Google's Code Jam (http://goo.gl/yX6J8).
import Data.List.Split (splitOn)
import Control.Monad ((>=>))
newtype Circle = Circle { circleRadius :: Double } deriving (Eq, Show)
newtype Paint = Paint { remainingMl :: Double } deriving (Eq, Show)
data Ring = Ring { ringRadius :: Double
, ringThickness :: Double
} deriving (Eq, Show)
@shoooe
shoooe / state_sequences.hs
Last active August 29, 2015 14:01
Playing around with the monad State. The program finds the number of sequences (defined as repetitions of a character) in a string.
import Control.Monad.State
data Sequence = Sequence { sequenceChar :: Char
, sequenceCount :: Integer } deriving (Eq, Show)
-- Without State monad.
-- Unused.
getNextSequence :: String -> (Sequence, String)
getNextSequence (h : []) = (Sequence h 1, [])
getNextSequence (h : ss) =
@shoooe
shoooe / lottery.hs
Created May 9, 2014 01:07
Simple algorithm to draw 6 numbers in the range [1..49], implemented in Haskell, as an exercise with Data.Set.
import qualified Data.Set as Set
import System.Random (randomRIO)
import Control.Monad (return)
type Numbers = Set.Set Int
draw :: Int -> Numbers -> IO Numbers
draw i s =
if ((Set.size s) == i) then return s
else do
@shoooe
shoooe / apply.hpp
Last active August 29, 2015 14:01
Simple implementation for partial function application in C++ (only works without overloaded functions, of course).
#include <utility>
template<class Func, class... Args>
auto apply(Func fn, Args&&... args) {
return [&](auto&&... sargs) {
return fn
( std::forward<decltype(args)>(args)...
, std::forward<decltype(sargs)>(sargs)... );
};
}
@shoooe
shoooe / renderable.hs
Created June 2, 2014 20:45
Just let it stay here for a while.
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE UndecidableInstances #-}
import Data.Map (Map)
import Data.List (intersperse)
import qualified Data.Map as Map
class Renderable a where
render :: a -> String