Skip to content

Instantly share code, notes, and snippets.

View diegopacheco's full-sized avatar

Diego Pacheco diegopacheco

View GitHub Profile
@diegopacheco
diegopacheco / up.hs
Created March 6, 2012 04:59
Haskell List Compreenshions Chars
import Data.Char (toUpper)
up :: [Char] -> [Char]
up s = [toUpper c | c <- s]
@diegopacheco
diegopacheco / boom.hs
Created March 6, 2012 05:05
Haskell List Compreenshions - IFS
boomBangs xs = [ if x < 10 then "BOOM!" else "BANG!" | x <- xs, odd x]
boomBangs [7..13] -- ["BOOM!","BOOM!","BANG!","BANG!"]
@diegopacheco
diegopacheco / scala-compre.scala
Created March 6, 2012 05:12
Scala List Comprehensions
def even(from: Int, to: Int): List[Int] = for (i <- List.range(from, to) if i % 2 == 0) yield i
Console.println(even(0, 20)) // List(0, 2, 4, 6, 8, 10, 12, 14, 16, 18)
@diegopacheco
diegopacheco / scala-compre2.scala
Created March 6, 2012 05:14
Scala List Comprehensions 2
for (i <- Iterator.range(0, 20);
j <- Iterator.range(i + 1, 20) if i + j == 32)
println("(" + i + ", " + j + ")")
// (13, 19)
// (14, 18)
// (15, 17)
@diegopacheco
diegopacheco / lots-lc.scala
Created March 6, 2012 05:22
Scala lots of List Comprehensions
for( x <- ( 1 to 5 ) ) yield x*x // scala.collection.immutable.IndexedSeq[Int] = Vector(1, 4, 9, 16, 25)
@diegopacheco
diegopacheco / maybe.hs
Created March 6, 2012 06:21
Haskell Maybe Monads
Just 5 >>= (\ x -> if (x == 0) then fail "zero" else Just (x + 1) ) -- Just 6
Just 0 >>= (\ x -> if (x == 0) then fail "zero" else Just (x + 1) ) -- Nothing
Nothing >>= (\ x -> if (x == 0) then fail "zero" else Just (x + 1) ) -- Nothing
def funcMonadic[A](f1: () => A) = new {
def andThen28[B](f2: () => B) = { f1() ; f2() }
}
def monadic[A](f1: () => A) = new {
def ->[B](f2: () => B) = { f1() ; f2() }
}
def bigMonadicPipe() = {
monadic( () => print("A") ) -> ( () => print("B") ) -> ( () => print("C") )()
@diegopacheco
diegopacheco / fm.hs
Created March 6, 2012 23:27
Haskell fmap Functor Monads
fmap (++"!") (Just "wisdom") -- Just "wisdom!"
fmap (++"!") Nothing -- Nothing
applyMaybe :: Maybe a -> (a -> Maybe b) -> Maybe b
applyMaybe Nothing f = Nothing
applyMaybe (Just x) f = f x
Just "smile" `applyMaybe` \x -> Just (x ++ " :)") -- Just "smile :)"
@diegopacheco
diegopacheco / monad.hs
Created March 6, 2012 23:37
Haskell Monad
class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b
(>>) :: m a -> m b -> m b
x >> y = x >>= \_ -> y
fail :: String -> m a
fail msg = error msg
@diegopacheco
diegopacheco / haskell-monad-maybe.hs
Created March 6, 2012 23:41
Monad Maybe Haskell
instance Monad Maybe where
return x = Just x
Nothing >>= f = Nothing
Just x >>= f = f x
fail _ = Nothing