Created
July 24, 2011 08:02
-
-
Save tomykaira/1102396 to your computer and use it in GitHub Desktop.
#start_haskell サンプルプログラム1
This file contains hidden or 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
Shape: データ名 | |
Rectangle: constructor | |
先頭がおおもじなのは型クラス名です | |
> data Shape = Rectangle Float Float | |
> | Square Float | |
> | Circle Float | |
> | Triangle Float Float | |
これについての関数を定義 | |
Rectangle はコンストラクタでのパターンマッチ | |
l, w は代入 | |
> area :: Shape -> Float | |
> area (Rectangle l w) = l * w | |
> area (Square l) = area $ Rectangle l l | |
> area (Circle r) = r * r * pi | |
> area (Triangle b h) = b * h / 2 | |
円周をもとめたい | |
> perimeter :: Shape -> Float | |
> perimeter (Rectangle l w) = 2*l + 2*w | |
> perimeter (Square l) = perimeter $ Rectangle l l | |
> perimeter (Circle r) = 2 * pi * r | |
> perimeter (Triangle b h) = b + h + (sqrt (b^2+h^2)) | |
let や where をつかうこともできる | |
let a = x | |
b = y | |
in 計算 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment