Skip to content

Instantly share code, notes, and snippets.

View shoooe's full-sized avatar
🏓

Thomas Pigarelli shoooe

🏓
View GitHub Profile
@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

####Recurrent Standard C++ quotes

This is a list of quotes from the C++ standard that are often useful while answering questions on StackOverflow. Feel free to suggest quotes that should be added.

The following quotes refer to the N3936 draft (~C++14).


§1.9/15 Program execution [intro.execution]

Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced. [ Note: In an expression that is evaluated more than once during the execution of a program, unsequenced and indeterminately sequenced evaluations of its subexpressions need not be performed consistently in different evaluations. — end note ] The value computations of the operands of an operator are sequenced before the value computation of the result of the operator. If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computatio

@shoooe
shoooe / matrix_fun.hs
Last active August 29, 2015 14:03
Some fun with matrices.
data Matrix a
= Matrix (Int, Int) [[a]]
deriving (Eq, Show)
rowIndexes :: Matrix a -> [Int]
rowIndexes (Matrix (_, y) _) = [0..y - 1]
colIndexes :: Matrix a -> [Int]
colIndexes (Matrix (x, _) _) = [0..x - 1]
@shoooe
shoooe / at_least_two_equal_digits.hs
Created June 29, 2014 14:24
Simple program that calculates the number of possible phone numbers of length 4 that have 2 or more equal digits, and displays them.
import Data.Map (Map)
import qualified Data.Map as Map
countDistinct :: Ord a => [a] -> Int
countDistinct = Map.size . foldr fn Map.empty
where fn e = Map.insertWith (+) e 1
validStrings :: [String]
validStrings =
filter ((`elem` [1..3]) . countDistinct) $ do
import Data.Char (toLower)
isPangram :: String -> Bool
isPangram s =
let ls = map (toLower) s
in null . filter (not . (`elem` ls)) $ ['a'..'z']
main :: IO ()
main = do
s <- getLine
@shoooe
shoooe / perms.hs
Last active August 29, 2015 14:09
Some exercise with Haskell
-- Original question can be found here:
-- http://www.reddit.com/r/haskell/comments/2lq8dt/produce_infinite_list_with_a_string/
permWithRep :: Int -> [a] -> [[a]]
permWithRep = replicateM
allSets :: String -> [[String]]
allSets str = map (flip permWithRep $ str) [0..]
main :: IO ()
@shoooe
shoooe / ExampleInput.txt
Last active August 29, 2015 14:10
Exercise that I should have done in Java, but I decided to do in 100 lines of Haskell.
0 A VUOTO 1 2 VUOTO
1 B VUOTO VUOTO 3 0
2 C 0 3 VUOTO VUOTO
3 D 1 VUOTO VUOTO 2