2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

99 PSiE [Problem 1]

2
Last updated at Posted at 2019-02-11

はじめに

Ninety-nine Problems, Solved in Elmというコンテンツが世の中にはあるらしい。 Elm の勉強をしようと思っても、なかなか参考にできるものは日英を問わず数が少ない。そこで Qiita という場所で、アウトプットをして、第三者に添削してもらおうと思い立ち、投稿することにした。

おことわり

  • 上記のコンテンツは Elm 0.19.0 用に書かれたものではない。この記事の執筆者が『副作用』を無視し、かつ改変したものを記述している。そのため原著作者の想定したものと異なるものを書いている可能性がある。
  • もともとはブログに書こうと思っていたのだが、個人的に Jekyll + Markdown が生理的に受け付けなくなったので、Qiita で書くことにした。というわけでこの記事は自分のホームページが再開したなら、そちらの方に移行する可能性があるので、予めご了承いただきたい。

当方の環境について

$ uname -msr 
> Darwin 18.2.0 x86_64
$ elm --version
> 0.19.0

問題

Write a function last that returns the last element of a list. An empty list doesn't have a last element, therefore last must return a Maybe.

成果物


last : List a -> Maybe a
last xs =
    let
        sample = last_ xs
    in
        case sample of
            [] -> Nothing
            _ -> List.head sample

last_ : List a -> List a
last_ xs =
    case xs of
        (y::ys) -> if List.length ys == 0 then [y] else last_ ys
        [] -> []

正解

=> Problem 1 Solutions

コメント

関数を2つに分けて解くというダサいことをしてしまった。あんまり Elm の case 文を理解していないようだ。猛省せねば。

2
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?