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
@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

5 高階関数 課題

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

length' :: [a] -> Int

all' :: (a -> Bool) -> [a] -> Bool 
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
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)