-
Notifications
You must be signed in to change notification settings - Fork 0
Chapter07回答 #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
44103
wants to merge
1
commit into
master
Choose a base branch
from
develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Chapter07回答 #3
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| defmodule MyList do | ||
| def sum(list), do: _sum(list) | ||
|
|
||
| defp _sum([]), do: 0 | ||
| defp _sum([head | tail]), do: head + _sum(tail) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| defmodule MyList do | ||
| def mapsum(list, func), do: _mapsum(list, func) | ||
|
|
||
| defp _mapsum([], _func), do: 0 | ||
| defp _mapsum([head | tail], func) do | ||
| func.(head) + _mapsum(tail, func) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| defmodule MyList do | ||
| def max(list), do: _max(list, -1) | ||
|
|
||
| defp _max([], value), do: value | ||
| defp _max([head | tail], value) when head > value do | ||
| _max(tail, head) | ||
| end | ||
| defp _max([head | tail], value) when head <= value do | ||
| _max(tail, value) | ||
| end | ||
| end | ||
|
|
||
| # defmodule MyList do | ||
| # def max(list), do: _max(list) | ||
|
|
||
| # defp _max([head | []]), do: head | ||
| # defp _max([head | tail]) when head > _max(tail) do | ||
| # _max(tail) | ||
| # head | ||
| # end | ||
| # defp _max([head | tail], value) when head <= _max(tail) do | ||
| # _max(tail) | ||
| # value | ||
| # end | ||
| # end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| defmodule MyList do | ||
| def caesar(list, n), do: _caesar(list, n) | ||
|
|
||
| defp _caesar([], n), do: [] | ||
| defp _caesar([head | tail], n) do | ||
| [a, z] = 'az' | ||
| [rem(head-a+n, z-a+1)+a | _caesar(tail, n)] | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| defmodule MyList do | ||
| def span(from, to), do: _span(from, to) | ||
|
|
||
| defp _span(from, to) when from == to do | ||
| [to] | ||
| end | ||
| defp _span(from, to) when from < to do | ||
| [from | _span(from+1, to)] | ||
| end | ||
| defp _span(from, to) when from > to do | ||
| [from | _span(from-1, to)] | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| defmodule MyList do | ||
| def len([]), do: 0 | ||
| def len([_head | tail]), do: 1 + len(tail) | ||
|
|
||
| def square([]), do: [] | ||
| def square([head | tail]), do: [head*head | square(tail)] | ||
|
|
||
| def add_1([]), do: [] | ||
| def add_1([head | tail]), do: [head+1 | add_1(tail)] | ||
|
|
||
| def map([], _func), do: [] | ||
| def map([head | tail], func), do: [func.(head) | map(tail, func)] | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| defmodule MyList do | ||
| def sum(list), do: _sum(list, 0) | ||
|
|
||
| # プライベート関数 | ||
| defp _sum([], total), do: total | ||
| defp _sum([head | tail], total), do: _sum(tail, head+total) | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
第12章先取りになっちゃうけど,ガードでやるかcaseでやるかifでやるかは諸説あるので,別の書き方もあるって感じで書いときます
ただ,ページエラーの404とか分けるときはcaseが良さそうみたいなのを聞きました.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど。
ifは第10章で出てきてることを確認しました。case,if共に出てきたときにぜひ、それぞれの利点をお聞きしたいです。