Skip to content

Instantly share code, notes, and snippets.

@akanehara
akanehara / exercise1.hs
Created November 10, 2012 05:04
すごいHaskell勉強会 in 大阪 練習問題
-- しゅくだい exercise1.hs
-- ここから(だいたい)2章までの知識縛り
-- 中身が任意の型であること以外要求しないので型制約なし
null' :: [a] -> Bool
null' xs = if length xs == 0 then True else False
-- (+) を使うので Num a と制約
sum' :: Num a => [a] -> a
@akanehara
akanehara / listmonad.coffee
Created November 19, 2012 07:02
Listモナドこうですか?わかりません><
concat = (xss) ->
ys = []
for xs in xss
for x in xs
ys.push(x)
ys
# Listモナドのつもり
class List
constructor: (@xs) ->
@akanehara
akanehara / gist:4173981
Created November 30, 2012 05:50
すごいHaskell読書会 in 大阪 第2回 練習問題 答案 @akanehara
-- すごいHaskell読書会 in 大阪 第2回
-- 練習問題 答案 @akanehara
import Data.Char
-- 演習1
-- 与えられた文字列を大文字に変換する関数を書きなさい。
upperCase :: String -> String
upperCase [] = []
@akanehara
akanehara / listmonad.js
Created December 15, 2012 17:35
JavaScript の Array をモナドにしてみるテスト
var A = Array;
// (++) :: [a] -> [a] -> [a]
// | 既存の連結メソッド
A.prototype.concat;
// ------------------------------------
// Monad
// return :: Monad m => a -> m a
@akanehara
akanehara / fold.ml
Last active December 10, 2015 05:08
「fold書けないプログラマー」と聞いてドキっとしたので書いて確かめて気を落ち着かせる
let foldl f z xs =
let rec go acc xs =
match xs with
| [] -> acc
| x :: xs -> go (f acc x) xs
in go z xs;;
let foldr f z xs =
let rec go xs =
match xs with
@akanehara
akanehara / fold.scala
Last active December 10, 2015 05:08
「fold書けないプログラマー」と聞いてドキっとしたので書いて確かめて気を落ち着かせる
def foldr[A, B](f:(A, B) => B)(z:B)(xs:List[A]):B = {
def go(xs:List[A]):B = xs match {
case Nil => z
case x :: rest => f(x, go(rest))
}
go(xs)
}
def foldl[A, B](f:(A, B) => A)(z:A)(xs:List[B]):A = {
def go(acc:A, xs:List[B]):A = xs match {
@akanehara
akanehara / mu.hs
Created December 27, 2012 09:38
漫画「ムーたち」で、ムー夫のママがドアノブの前で動かなくなってしまうシーンをHaskellで書くとたぶんこう
module Main (main) where
main :: IO ()
main = do
let fingersPermutation = permutation fingers
mapM_ (putStrLn . show) fingersPermutation
putStrLn $ show $ length fingersPermutation
fingers :: [Int]
fingers = [1..5]
@akanehara
akanehara / sugoih3.hs
Created January 11, 2013 10:19
すごいHaskell読書会 in 大阪 #3 練習問題
-- すごいHaskell読書会 in 大阪 #3
-- 練習問題
-- 問題1
-- 次のリスト内包を **map** と **filter** で書き直してみましょう
-- [ x ^ 2 | x <- [1..5], odd x]
-- 問題2
-- 標準関数 **takeWhile'** と **dropWhile'** を実装してみましょう。
@akanehara
akanehara / sugoih3ans.hs
Created January 11, 2013 12:21
すごいHaskell読書会 in 大阪 #3 練習問題解答例
-- すごいHaskell読書会 in 大阪 #3
-- 練習問題
-- 解答例
-- 問題1
-- 次のリスト内包を **map** と **filter** で書き直してみましょう
-- [ x ^ 2 | x <- [1..5], odd x]
map (^ 2) (filter odd [1..5])
@akanehara
akanehara / sugoih4.hs
Created January 30, 2013 07:40
すごいHaskell読書会 in 大阪 #4 第6章 「モジュール」持ち寄り練習問題
-- すごいHaskell読書会 in 大阪 #4
-- 第6章 モジュール
-- 持ち寄り練習問題 @akanehara
-- このスクリプトでは、英語圏でよく冗談めいて使われる
-- 換字式暗号ROT13(http://ja.wikipedia.org/wiki/ROT13)
-- の動作を確かめて遊んでいます。
-- ところが、ここで使われているモジュール Toys.Rot13
-- はまだ実装されていません!
-- testRot13 が True を返すように、Toys.Rot13 を実装