LoginSignup
0
0

More than 5 years have passed since last update.

99 PSiE [Problem 8]

Posted at

はじめに

Ninety-nine Problems, Solved in Elm を毎日ふれることで、Elm に親しもうという『日記』です。詳細は 99 PSiE [Problem 1] をご覧ください。

問題

Write a function to remove consecutive duplicates of list elements.
(引用元:Problem 8)

成果物

-- Ninety-nine Problems, Problem 8
-- https://johncrane.gitbooks.io/ninety-nine-elm-problems/content/p/p08.html
import Html

-- main = Html.text
--        <| Debug.toString
--        <| noDupes [1, 1, 2, 2, 3, 3, 3, 4, 5, 4, 4, 4, 4]

-- noDupes list =
--     case list of
--         x::xs ->
--             case xs of
--                 y::ys -> if x == y then
--                              [x] ++ noDupes ys
--                          else
--                              [x] ++ [y] ++ noDupes ys
--                 [] -> []
--         [] -> []

-- noDupes list = List.foldl (\x xs -> case xs of
--                                         y::ys -> ) [] list
noDupes list =
    let
        noDupCons y ys =
            case List.head ys of
                Nothing -> [y]
                Just a ->
                    if y == a then
                        ys
                    else
                        y::ys
    in
        List.foldr noDupCons [] list

-- Checker
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)
            [ noDupes [ 1, 1, 1, 1, 2, 5, 5, 2, 1 ] == [ 1, 2, 5, 2, 1 ]
            , noDupes [ 2, 1, 1, 1 ] == [ 2, 1 ]
            , noDupes [ 2, 2, 2, 1, 1, 1 ] == [ 2, 1 ]
            , noDupes [ 1 ] == [ 1 ]
            , noDupes [] == []
            , noDupes [ "aa", "aa", "aa" ] == [ "aa" ]
            , noDupes [ "aab", "b", "b", "aa" ] == [ "aab", "b", "aa" ]
            ]

正答例

=> Problem 8 Solutions

コメント

  • "consecutive" って連続って意味なのか...
  • 解けなかった。くそったれ。
  • あぁ、Maybe つかうのか。
  • しかし Elm はなぜ Haskell で標準で利用できる関数群を消去していったのか。謎は深まるばかりだ。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0