Haskellのガード勉強中

before

between :: Int -> Int -> Int -> Bool
between x y z = 
    if ((y < x && x < z) || (z < x && x < y)) then
        True
    else
        False

after

between :: Int -> Int -> Int -> Bool
between x y z  
    | y < x && x < z = True
    | z < x && x < y = True
    | otherwise = False

before

progress_turn :: Int -> Int -> Int -> [Int]
progress_turn x y z =
    if (between x y z) then
        [x, 0, 0]
    else 
        if (between y x z) then
            [0, y, 0]
        else
            [0, 0, z]

after

progress_turn :: Int -> Int -> Int -> [Int]
progress_turn x y z
    | (between x y z) = [x, 0, 0]
    | (between y x z) = [0, y, 0]
    | otherwise = [0, 0, z]

before

your_score :: [Int] -> [Int] -> Int
your_score cur_score new_score = 
    if (x == y && x == z) then 
        0
    else 
        if (x == y || x == z) then
             1
        else 
            if (between x y z) then
                2
            else
                if (y == z) then
                    -2
                else
                    -1

    where [x, y, z] = [x + y | (x, y)<-(zip cur_score new_score)]

after

your_score :: [Int] -> [Int] -> Int
your_score cur_score new_score 
    | (x == y && x == z) = 0
    | (x == y || x == z) = 1
    | (between x y z) = 2
    | (y == z) = -2
    | otherwise = -1
    where [x, y, z] = [x + y | (x, y)<-(zip cur_score new_score)]

おおお、ガードを使うととてもきれいに書けるな。

Haskell続き

眠さを通り越してハイになってきた。

id:ujihisa progress_turnが[Int]を返すのは妥当なのかなあ。(Int, Int, Int)のがいいきが。

data Score = Score Int Int Int deriving Show
add :: Score -> Score -> Score
add (Score x y z) (Score a b c) = (Score (x + a) (y + b) (z + c))
main = print $ (Score 1 2 3) `add` (Score 0 3 0)

こんなのにした。

おなかすいた

今朝2時頃に「気がついたら昨日何も食べていない!」っていいながら、でも別に空腹でもなかったので食べなかったんだが、今すごくおなかがすいてきた。

冷蔵庫の中の少し表面が黒くなった人参とか、キャベツとか、ニラとかを刻んでいためている。

しまった。一応火の通りにくい順ににんじん、きゃべつ、ニラ、って入れたんだけど、切りながら入れて行っただけなので、ニラなんか切るのに時間がかからないからキャベツと大差ない時間に入れてしまった。まずはキャベツが半透明になるくらいまで待ってから入れるべきだった。ニラなのかほうれん草なのかわからない状態になってしまった。


まぁ、そんなこともあったけど、朝から野菜たっぷりのチキンラーメンを食べて結果オーライ。