Help us understand the problem. What is going on with this article?

99 PSiE [Extra 2]

More than 1 year has passed since last update.

はじめに

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

問題

Haskell の takeWhile を Elm で実現せよ。
(Extra 2を改変)

成果物

import Html

-- My solution
takeWhile__ func list =
    let
        takeWhile_ func_ evaluatedList notEvaluatedList =
            case notEvaluatedList of
                y::ys -> if func_ y then
                             takeWhile_ func_ (evaluatedList ++ [y]) ys
                         else
                             evaluatedList
                [] -> evaluatedList
    in
        takeWhile_ func [] list


-- Their solution.
takeWhile predicate xs =
    case xs of
        [] -> []
        y::ys -> if predicate y then
                    y :: takeWhile predicate ys
                else
                    []

-- Checker
isOdd x =
    modBy 2 x /= 0
isEven x =
    modBy 2 x == 0

-- main = takeWhile ((>) 50) (List.range 1 10)
--         |> Debug.toString
--         |> Html.text

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)
            [ takeWhile isOdd [ 1, 2, 1 ] == [ 1 ]
            , takeWhile isEven [ 1, 2, 1 ] == []
            , takeWhile isEven [] == []
            , takeWhile isEven [ 2, 4, 100000, 1 ] == [ 2, 4, 100000 ]
            , takeWhile ((>) 5) (List.range 1 10) == [ 1, 2, 3, 4 ]
            , takeWhile ((>) 50) (List.range 1 10) == List.range 1 10
            ]

正答例

Extra 2 Solution

コメント

けっこう時間がかかった。let 〜 in の束縛うまく使いこなすのが存外に難しい。引数にデフォルト値をとれるなら、let 〜 in の束縛が不要なんだがなぁ... これ Ruby 脳なのかなぁ... まぁ、正解例は let 〜 in 不要なのでしたがね。

seahal
んha-
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away