Skip to content

Instantly share code, notes, and snippets.

View tan-yuki's full-sized avatar

tan-yuki tan-yuki

  • Chatwork
  • Japan, Tokyo
View GitHub Profile
import System.Random
dice :: StdGen -> Int
dice gen = num
where (num, _) = diceWithRandomGen gen
diceWithRandomGen :: StdGen -> (Int, StdGen)
diceWithRandomGen = randomR (1, 6)
threeTimesDiceRoll :: StdGen -> (Int, Int, Int)
length' :: String -> Int
length' = foldl (\ x _ -> x + 1) 0
last' :: Show a => [a] -> (Maybe a)
last' = foldl (\ _ x -> Just x) Nothing
head' :: Show a => [a] -> (Maybe a)
head' [] = Nothing
head' (x:_) = Just x

5 高階関数 課題

下記length', all'foldl, foldr, foldl1, foldr1のいずれかを用いて実装せよ。 length', all'の挙動はlength, allを参照すること。

length' :: [a] -> Int

all' :: (a -> Bool) -> [a] -> Bool 
@tan-yuki
tan-yuki / haskell7.md
Last active August 29, 2015 14:22
すごいHaskell楽しく学ぼう 7章

型や型引数を自分でつくろう

7.2 形づくる

  • 宿題
    • 3点のPointから、それらを点を結んだ時の三角形の面積を求める関数を実装してください。
data Point = Point Float Float
data Triangle = Triangle Point Point Point
@tan-yuki
tan-yuki / haskell6.md
Last active August 29, 2015 14:22
すごいHaskell楽しく学ぼう 6章

6章モジュール

復習) 型と型クラス

Prelude> :t sum
sum :: Num a => [a] -> a
       ^^^
       型クラス
@tan-yuki
tan-yuki / haskell55-57.md
Last active August 29, 2015 14:22
すごいHaskellたのしく学ぼう 5.5 - 5.7

5.5 畳み込み、見込みあり!

foldrで右畳み込み

map' :: (a -> b) -> [a] -> [b]
map' f xs = foldr (\x acc -> f x : acc) [] xs
-- acc
-- []
-- [6]
@tan-yuki
tan-yuki / haskell_15day.md
Last active August 29, 2015 14:04
haskell_15day.md

データ型

++

infixr 5 ++ -- infixr: 結合度
(++) :: [a] -> [a] -> [a]
[]     ++ ys = ys
(x:xs) ++ ys = x : (xs ++ ys)
@tan-yuki
tan-yuki / haskell_11day.md
Last active August 29, 2015 14:02
2013/06/14(土)にOSSCafeでおこなったhaskell勉強会のメモ

振り返り

import

import Data.List

numUniques :: (Eq a) => [a] -> Int
numUniques = length . nub
_.mixin({
sum: function(list, callback) {
return _.reduce(list, function(memo, item) {
return memo + callback(item);
}, 0);
}
});
// == test ==
// _.sum([{price:10}, {price:20}, {price:30}], function(item) { return item.price; });