はじめに
Ninety-nine Problems, Solved in Elm を毎日さわることで、Elm に親しもうという『日記』です。詳細は 99 PSiE [Problem 1] をご覧ください。
問題
Determine if a list is a palindrome, that is, the list is identical when read forward or backward.
(引用元: Problem 6)
成果物
import Html
main : Html.Html a
main =
Html.text
<| case test of
0 -> "Your implementation passed all tests."
1 -> "Your implementation failed one test."
x -> "Your implementation failed " ++ Debug.toString x ++ " tests."
test : Int
test =
List.length
<| List.filter ((==) False)
[ isPalindrome [ 1, 3, 5, 8, 5, 3, 1 ] == True
, isPalindrome [ 2, 1 ] == False
, isPalindrome [ 1 ] == True
, isPalindrome [] == True
, isPalindrome [ "aa", "bb", "aa" ] == True
, isPalindrome [ "aab", "b", "aa" ] == False
]
-- My solution 1.
isPalindrome_ list =
list == List.reverse list
-- 最初は中央と左右にわけて、左右比較するという方法を思いついたが、普通にひっくり返した方楽なことに気がついてこういう形となった
-- Their Solution 2
isPalindrome list =
let
halfLength = List.length list // 2
left = List.take halfLength list
right = List.take halfLength (List.reverse list)
in
right == left
正答
コメント
Haskell みたいな !! がないのは不利かと思っていたが、そうでもないみたいですね。
P.S.
なぁ、思わないか? Vue や React なんかが全盛の昨今よりも、あの jQuery の時代のほうがマシだったと。むしろ Html なんて、jQuery ぐらいで終わらせておくべきだったと。