Skip to content

Instantly share code, notes, and snippets.

View blouerat's full-sized avatar

Bastien Louërat blouerat

View GitHub Profile
@blouerat
blouerat / FreeIdZero.scala
Created May 2, 2014 16:18
Scala, Free Monad and TAPL
import scalaz.Functor
import scalaz.Free
sealed trait Term[+A]
object Term {
case object Zero extends Term[Nothing]
case class Id[A](term: A) extends Term[A]
implicit val termFunctor: Functor[Term] = new Functor[Term] {
def map[A, B](fa: Term[A])(f: A => B): Term[B] = fa match {
@blouerat
blouerat / appendSwapped.hs
Last active August 29, 2015 14:01
1HaskellADay: appendSwapped
import Control.Applicative
import Data.Tuple (swap)
{- Given a list of tuples, return a list containing the initial elements and the swapped elements
It's an easy one to start the week.
Example
> appendSwapped [(1,2),(2,3)]
[(1,2),(2,3),(2,1),(3,2)]
-}
@blouerat
blouerat / interleaveSwapped.hs
Last active August 29, 2015 14:01
1HaskellADay: interleaveSwapped
import Control.Applicative
import Data.Tuple
{- Yesterday, we appended swapped values:
Example
> appendSwapped [(1,2),(2,3)]
[(1,2),(2,3),(2,1),(3,2)]
-}
appendSwapped :: [(a,a)] -> [(a,a)]
@blouerat
blouerat / countOccurrences.hs
Created June 2, 2014 09:51
1HaskellADay: countOccurrences
import Data.Map hiding (foldr)
{-| Count character occurrences in a string
Examples:
>>> lookup 'l' $ countOccurrences "hello"
Just 2
>>> lookup 'n' $ countOccurrences "hello"
Nothing
-}
@blouerat
blouerat / collect.hs
Last active August 29, 2015 14:02
1HaskellADay: collect (a room traversal)
import Control.Applicative
{- You have a list of rooms, each rooms has two doors and contain one element.
To enter a room, the first door must be opened. To leave a room, the second door
must be open. It means that if you want to move from one room to the next one,
both the second door of the first room and the first door of the second room.
-}
data Room a = Room (Bool, Bool) a
{- | Given a list of rooms, we want to go as far as we can and collect the elements
@blouerat
blouerat / collectReverse.hs
Created June 17, 2014 07:42
1HaskellADay: collectReverse
import Control.Applicative
{- We still have yesterday's room
-}
data Room a = Room {doors :: (Bool, Bool), content :: a}
deriving (Eq)
{- | Given a list of rooms, we want to go as far as we can and collect the elements
in he room, **starting from the last room**.
@blouerat
blouerat / iso1.hs
Last active August 29, 2015 14:02
1HaskellADay: iso1
{-# LANGUAGE TupleSections #-}
-- Follow the type and the properties
f :: Either a a -> (Bool, a)
f = either (False,) (True,)
g :: (Bool, a) -> Either a a
g (False, a) = Left a
g (True, a) = Right a
@blouerat
blouerat / keepEqual.hs
Created June 20, 2014 07:43
1HaskellADay: keepEqual
{-| Keep equal elements that are at the same position in both lists
Examples:
>>> keepEqual "hello" "world"
"l"
>>> keepEqual (repeat 1) [0..10]
[1]
@blouerat
blouerat / roll.hs
Created July 7, 2014 11:32
1HaskellADay: roll
import Data.Tuple
{--
Okay, shoot! Fell asleep while composing a problem; sorry for the delay!
So, this one is an easy one. We get a little Forth'y'. This is from P19
of the P99 problem-set.
--}
@blouerat
blouerat / Hadrien
Created July 21, 2014 17:40
Mémoires d'Hadrien, Marguerite Yourcenar
« Mais c'est précisément parce que j'attends peu de chose de la condition humaine, les périodes de bonheur, les progrès partiels, les efforts de recommencement et de continuité me semblent autant de prodiges qui compensent presque l'immense masse des maux, des échecs, de l'incurie et de l'erreur. Les catastrophes et les ruines viendront ; le désordre triomphera, mais de temps en temps l'ordre aussi. La paix s'installera de nouveau entre deux périodes de guerre ; les mots de liberté, d'humanité, de justice retrouveront çà et là le sens que nous avons tenté de leur donner. »
– Marguerite Yourcenar, « Mémoires d'Hadrien »