-
-
Notifications
You must be signed in to change notification settings - Fork 115
Add forked concept recursion and concept exercise bird-count
#750
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
Conversation
recursion and concept exercise birdCountrecursion and concept exercise bird-count
concepts/recursion/about.md
Outdated
| Due to immutability, loops in Elm are written differently from imperative languages. | ||
| For example, loops commonly look like: | ||
|
|
||
| ``` |
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.
Maybe mark this as c or something?
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.
It's not C, because of array.length. I'll mention it's pseudocode.
EDIT: eh, it doesn't matter, I'll mark it as C for the highlighting.
| ``` | ||
|
|
||
| A recursive function can have many base cases and/or many recursive cases. | ||
| For example [the Fibonacci sequence][fibonacci] is a recursive sequence with two base cases: |
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.
I think this is a famously slow way to solve the fibonacci sequence. Probably worth mentioning that or choosing a different example?
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.
I couldn't find a different example that was as simple as this one and not terribly slow, so I added a note.
concepts/recursion/about.md
Outdated
|
|
||
| Recursive functions, if implemented incorrectly, might never return their result. | ||
| This can be problematic because each time a function is called, a reference is stored in memory where the runtime should return the result (on the [call stack][wiki-call-stack]). | ||
| If a recursive function calls itself infinitely, it is possible to run out of memory causing the runtime to crash (a [stack overflow error][wiki-stack-overflow]). |
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.
This can also happen for large / inefficient cases, but can't happen if tail call elimination happens.
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.
I reworked the text to mention large cases, but I want to keep tail call elimination for the next concept.
That being said, for infinite recursion, tail call optimization can be a negative thing, because it won't overflow, it'll keep heating up your CPU for no benefit :)
|
|
||
| ## General | ||
|
|
||
| - Read about recursion [here][recursion]. |
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.
Using 'here' for links is an antipattern.
| - Read about recursion [here][recursion]. | |
| - Read about [recursion from the creator of Elm][recursion]. |
| You have chosen to store the data as a list of integers. | ||
| The first number in the list is the number of birds that visited your garden today, the second yesterday, and so on. | ||
|
|
||
| ```exercism/note |
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.
This is a shame, is there not an example of using recursion where it would be the idiomatic thing to do?
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.
Yes, I agree that it's a shame, however I generally think that it's a good exercise to re-implement core functions to deepen your understanding of them. With the understanding that it's not idiomatic of course, and in that sense, a concept exercise is the right place to do it.
Pretty much all practice exercise that are marked with recursion are examples where recursion is the idiomatic thing to do.
I do realize that I'm biased because I don't really want to come up with a completely new exercise, but I will:
- tweak the note to emphasize that this is not idiomatic, but for the sake of learning
- come up with an exercise for
tail-call-recursionthat uses recursion idiomatically
|
|
||
| today : List Int -> Maybe Int | ||
| today counts = | ||
| case counts of |
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.
I would use List.head here
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.
Right, but all these are pretty much one liners if you use List, except for incrementDayCount.
today = List.head
hasDayWithoutBirds = List.any ((==) 0)
total = List.sum
busyDays = List.filter ((>=) 5) >> List.length
Part of #692
I took this concept and exercise combo pretty much straight out of the Elixir track, which itself forked it from the C# track.
Some notes:
introduction.mdas TODO, but this time, I followed the lead of Elixir that includes more details in theabout.mdand less in the intro. (it's still missing in the exercise though, so the CI will fail)stringsandlistsfrom some exercises that practiced those, because we had to many, (bin/configlet lintwarns that we should have at most 10, now we are OK)