CLEANを学習してみる
遅延型関数型言語ということなのでHaskellのような感じか。
とりあえずチュートリアルの1部1章を頭で学習
map関数は
map :: (a->b) [a]->[b] map func [] = [] map func [head:tail] = [func head] ++ (map func tail)
でいいのかな。パラメータ型が小文字始まりでなくてはいけないのはアレらと同じだけど、どうなんでしょうね。
またインデントが意味を持ってるようで、シンタクス面でPythonっぽいところがある(どっちが先かは知らないけど)。haskellよりはなじみやすい文法だと思う。プログラミング言語の価値はほぼシンタックスとライブラリ環境で決まりますから、この差は重要です。
演習問題
1.
module tut1 import StdEnv Start = 42
2.
module tut2 import StdEnv pow x 1 = x pow x n = x * (pow x (n - 1)) Start = pow 128 2
3.
module tut3 import StdEnv isum :: Int -> Int isum 0 = 0 isum n = (n - div10 * 10) + (isum div10) where div10 = n / 10 Start = isum 1234
もしくは
module tut3 import StdEnv isum :: Int -> Int isum 0 = 0 isum n = (n % 10) + (isum div10) where (%) a b = a - (a / b) * b div10 = n / 10 Start = isum 1234
4.
module tut4 import StdEnv isum 0 = 0 isum n = (n % 10) + (isum div10) where (%) a b = a - (a / b) * b div10 = n / 10 is9 n | n == 9 = True | n < 9 = False | otherwise = is9 (isum n) Start = is9 1234
5.
module tut5 import StdEnv mmax a b | a > b = a | otherwise = b Start = mmax 10 12
6.
module tut6 import StdEnv mmin a b | a < b = a | otherwise = b Start = mmin 10 12
7.
module tut7 import StdEnv MaxOfInt [] = 0 MaxOfInt [head:tail] = max head (MaxOfInt tail) where max a b | a > b = a | otherwise = b Start = MaxOfInt [10,11,12,13]
8.
module tut8 import StdEnv MinOfInt [a] = a MinOfInt [head:tail] = min head (MinOfInt tail) where min a b | a < b = a | otherwise = b Start = MinOfInt [10,11,12,13]
9.
module tut9 import StdEnv Last [a] = a Last [_:tail] = Last tail Start = Last [1,2,3,4,5]
10.
module tut10 import StdEnv LastTwo [a,b] = [a,b] LastTwo [_:tail] = LastTwo tail Start = LastTwo [1,2,3,4,5]
11.
module tut11 import StdEnv Reverse [] = [] Reverse [head:tail] = (Reverse tail) ++ [head] Start = Reverse [1,2,3,4,5]
12.
module tut12 import StdEnv Reverse [] = [] Reverse [head:tail] = (Reverse tail) ++ [head] Palindrome list = equalList list (Reverse list) where equalList [] [] = True equalList [] _ = False equalList _ [] = False equalList [ha:ta] [hb:tb] | ha <> hb = False | otherwise = equalList ta tb Start = Palindrome [1,2,3,2,1]
↓こういうことはできるのかな?できませんでした
module tut12 import StdEnv Palindrome [] = True Palindrome [_] = True Palindrome [a:b,c] | a == c = Palindrome b | otherwise = False Start = Palindrome [1,2,3,2,1]
リストパターンには、:のあとに,を混ぜられないようだ(前はOK)。
IDEでは、
- まずNew Fileでiclファイルを作る
- そのファイルの窓をえらんだまま、New Projectでprjファイルを作る
- ソースを書きCtrl+Sで保存
- Update and Runで実行
prjファイルを作らないとビルドできませんし、prjファイルを作るにはさきにiclファイルを作る必要があるようです。
また、moduleのimportさせるにはきちんとdefinition moduleしたdclファイルを作る必要がありそうです(未確認)。