Skip to content

Instantly share code, notes, and snippets.

@jsoffer
jsoffer / gist:170276
Created August 19, 2009 10:17
Monads: bind/return
-- Uso de "bind"
import Data.List.HT (chop)
import Control.Monad(liftM, foldM)
lineas :: IO ([[String]])
lineas =
(liftM -- (String -> [[String]]) -> (IO (String) -> IO ([[String]]))
((map (chop (== ','))).lines)) -- String -> [[String]]
. readFile -- IO (String)
@jsoffer
jsoffer / gist:172712
Created August 22, 2009 09:07
Gnuplot y punto fijo
-- número de pasos para aproximar -phi por punto fijo de x = 1/(x-1)
Prelude Graphics.Gnuplot.Simple> plotList [] $
map (\x -> let
nums = iterate (\k -> 1/(k-1)) x in
(\(a,b,c) -> c) $
head $
dropWhile (\(a,b,c) -> a /= b) $
zip3 (tail nums) nums [1..]) (map (/3000) [0..12000])
@jsoffer
jsoffer / gist:177430
Created August 29, 2009 08:16
Haskore
-- Ejemplo de construcción de melodía (serie y paralelo) en Haskore
-- Suena horrible, pero hace exactamente lo que tiene que hacer.
-- haskore-0.1
-- fileFromGeneralMIDIMusic
import Haskore.Interface.MIDI.Render
-- a..g ("la" a "sol")
import Haskore.Melody
@jsoffer
jsoffer / gist:184143
Created September 9, 2009 22:31
Máquina de Turing
Máquina de Turing
----------------------------
Compilar con '-package parsec'; ejemplo de compilación: ' $ ghc -package parsec -o turing turing.lhs '
El ejecutable recibe como primer argumento la descripción de una máquina de Turing, y como segundo
argumento una cadena inicial para la cinta.
Si la máquina finaliza escribe True si se alcanzó un estado de paro, False en otro caso.
En ambos casos escribe el estado final de la cinta, en el formato ([lado izquierdo],[lado derecho])
module Main where
data Registro = R {
valor :: Integer,
nombre :: String,
peso :: Float
} deriving Show
data Arbol a = Hoja | Rama a a deriving Show
-- The Song that Never Ends
Prelude> let song = stanza ++ song where stanza = "This is the song that doesn't end, Yes, it goes on and on, my friend. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because... "
module Main where
import Control.Monad.State
import Control.Monad
-- funciones base del generador de programas al azar
import System.Random
import System.Random.Shuffle
-- Forma directa de implementar una máquina de estado vía Control.Monad.State
-- Implementa acciones en secuencia; no espera caso final, ejecuta k veces la máquina
-- Modificado a partir de http://www.haskell.org/pipermail/beginners/2008-September/000275.html
module FibState where
import Control.Monad.State
import Control.Monad
-- ¿cual es el tipo de datos del estado?
/* foldl en C con hilos */
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <math.h>
import Ratio
-- función lineal; x --> (a,b) = ax + b
type FL = (Ratio Integer, Ratio Integer)
-- Composición
-- x --> (a, b) · (c, d) = c(ax + b) + d = (acx) + (bc + d)
-- (a, b) · (c, d) = (ac, bc + d)