Prelude> foldr (-) 1 [4,8,5]
0
Prelude> foldl (-) 1 [4,8,5]
-16
-
- ソースコードファイル
- 色々、ルールはあるけれど、数値を与えて計算するだけなら、以下を"MyTest.hs"というファイルで保存して
module MyTest
where
x = 5
y = 4
z = x^y
Prelude> :load "MyTest.hs"
[1 of 1] Compiling MyTest ( MyTest.hs, interpreted )
Ok, modules loaded: MyTest.
*MyTest> x
5
*MyTest> y
4
*MyTest> z
625
*MyTest>
-
- 関数
- 関数は、何かの値を受け取って、何かの値に対応付ける、その対応付けのルール
- 符合を返す関数を考えるとき、次のように書くと
- 「xが負なら-1、負ではなくて正なら1、そのどちらでもなければ0」と読める
- これを「負であるかいなかを判断せよ、そして、真なら-1、偽であったら、今度は正であるかいなかを判断せよ、そして、真なら1、偽であったら0」と読むと、『手続き的』
signum x =
if x < 0
then -1
else if x > 0
then 1
else 0
f x = x*x
g x = x+1
h x = (f . g) x
k x = (g . f) x
Prelude> :load "gousei.hs"
[1 of 1] Compiling Main ( gousei.hs, interpreted )
Ok, modules loaded: Main.
*Main> f 3
9
*Main> g 3
4
*Main> (f.g)3
16
*Main> h 3
16
*Main> (g.f) 3
10
*Main> k 3
10