Skip to content

Instantly share code, notes, and snippets.

-- project Euler problem 59
import Control.Arrow ((***))
import Data.Bits (xor)
import Data.Char (chr, ord, isPrint, isSpace, toLower)
import Data.List (group, sort, transpose, unfoldr)
-- mapPair (f,g) (x,y) = (f x, g y)
-- groupsOf n = takeWhile (not . null) . unfoldr (Just . splitAt n)
groupsOf n | n>0 = unfoldr $ \x -> if null x then Nothing else Just(splitAt n x)
split n = transpose . groupsOf n
-- O(n) revTake and revDrop using list-numeral alike Church-numeral
-- (from Richard O'Keefe's post to haskell-cafe, i.e.
-- http://www.haskell.org/pipermail/haskell-cafe/2010-September/083905.html)
-- | l |
-- +----------------+-----+
-- | l-n (_:m) n |
revTake n xs = drop' (drop n xs) xs
where drop' (_:m) (_:xs) = drop' m xs
@nfunato
nfunato / p17.hs
Created October 3, 2013 22:19
project euler problem 17
p17 = p17' [1..1000]
p17' = sum . map (length . filter(`notElem` " -") . itoa)
itoa i
| i < 0 = "minus " ++ itoa (-i)
| i < 20 = smalls i
| i < 100 = f (quotRem i 10) tens "-" smalls
| i < 1000 = f (quotRem i 100) ((++" hundred").smalls) " and " itoa
| i ==1000 = "one thousand"
| otherwise = error "unsupported range"
@nfunato
nfunato / sudoku.hs
Last active December 24, 2015 05:39
{-- Just a porting from http://norvig.com/sudoku.html, Dec 2011 by @nfunato --}
import Data.List ((\\), delete, nub, null)
import Data.Map ((!))
import qualified Data.Map as M -- Map, adjust, fold, fromList, toList
import Control.Exception (assert)
import Text.Printf (printf)
import Data.Maybe (mapMaybe)
import Control.Monad (foldM, msum)
type Square = (Char,Char)
@nfunato
nfunato / csv-parser.hs
Last active December 23, 2015 16:09
an exercise of Text.ParserCombinators.ReadP
-- CSV file parser (as an exercise of Text.ParserCombinators.ReadP)
-- 2013-09-21 @nfunato
import Text.ParserCombinators.ReadP
import Control.Applicative ((<$>), (<*>), (<*), (*>))
-- NOTE:
-- The code here is baesd on d.hatena.ne.jp/kazu-yamamoto/20100104/1262597082
-- which shows code for Parsec2, not ReadP
-- BUGS:
-- AA graph drawer (as an exercise of mapAccum)
-- 2013-09-16 @nfunato
import Data.List (mapAccumL, transpose)
main = plot =<< getContents
plot = mapM_ putStrLn . convert
convert = transpose . makeVstrs . analyze . parse
parse = map cconv . filter (`elem` "RCF")
where cconv 'R' = (-1, '/' )
cconv 'C' = ( 0, '_' )
@nfunato
nfunato / Memo.hs
Last active December 22, 2015 14:08
{-# LANGUAGE FlexibleInstances #-}
-- ==================================================================
-- Memo module
-- the code from http://www.sampou.org/cgi-bin/haskell.cgi?Memoise
-- with non-essential patches by @nfunato on 2013-09-07
--
module Memo
(
@nfunato
nfunato / _contents
Last active December 21, 2015 04:09
Handy implementations of famous macros for Common Lisp (and-let, destructure-case)
with-gensyms, once-only (with-gensyms.lisp -- from Practical Common Lisp by Peter Seibel)
destructure-case (destructure-case.lisp)
and-let* (and-let.lisp)
@nfunato
nfunato / life-rosetta.fth
Last active March 4, 2025 18:19
Conway's Game of Life in FORTH (essentially same with one in rosesttacode)
\ -*- Mode: Forth -*-
\ Conway's Game of Life
\ originally from http://rosettacode.org/wiki/Conway's_Game_of_Life#Forth
\ see also http://en.wikipedia.org/wiki/Conway's_Game_of_Life
\ -------------------------------------------------------------------
\ The fast wrapping requires dimensions that are powers of 2.
\ (for playing just size, you may set terminal size to 64x17)
@nfunato
nfunato / forth-web-pages.html
Last active December 12, 2015 02:38
Some Forth pages to go
<!DOCTYPE html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="ja" dir="ltr">
<html>
<head>
<meta charset="UTF-8">
<title>
Some Forth pages to go
</title>
</head>
<body>