This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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 | |
( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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, '_' ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-- 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- project Euler problem 14 | |
import Data.Array (Array,(!),listArray,assocs) | |
import Data.List (foldl1',unfoldr) | |
import Data.Tuple (swap) | |
type Val = Int -- you might want to use Integer, not Int, for 32bit runtime | |
collatzLengths :: Int -> Array Int Val | |
collatzLengths nmax = arr | |
where arr = listArray (1,nmax) (1:[cL i| i<-[2..nmax]]) | |
cL n = f n 0 | |
where f i c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* Common Lisp tips, by Zach Beane: lisptips.com | |
** Runtime | |
*** The four causes of symbol conflicts | |
*** Putting the R in REPL | |
*** Evaluating the last expression | |
*** Describing objects | |
** Data and Control Flow | |
*** How do I apply AND? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; -*- Mode:Lisp ; Syntax:Common-Lisp -*- | |
;;; CL-ULFE: unix-like filter elements, by @nfunato on 2015-04-15 | |
;;; - placed in the public domain unless otherwise noted | |
;;; - usually assumed to be used with CL-PPCRE | |
;(ql:quiciload 'cl-ppcre) | |
(require 'cl-ppcre) | |
(defvar *case-fold-p* t) |