Skip to content
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

Add proverb exercise (#297) #303

Merged
merged 4 commits into from
Oct 30, 2023
Merged

Conversation

Adrien-LUDWIG
Copy link
Contributor

I had some hesitation for the formatting of example.rkt. What is the best option?

  • Current:
(define (recite lst)
  (cond
    [(empty? lst) '()]
    [else (define (verse index)
            (cond
              [(= index (sub1 (length lst))) (list (format "And all for the want of a ~a." (list-ref lst 0)))]
              [else (append
                     (list (format "For want of a ~a the ~a was lost."(list-ref lst index)(list-ref lst (add1 index))))
                     (verse (add1 index)))]))
          (verse 0)]))
  • Define the inner function before cond to reduce indentation, but verse is defined even if lst is empty:
(define (recite lst)
  (define (verse index)
    (cond
      [(= index (sub1 (length lst))) (list (format "And all for the want of a ~a." (list-ref lst 0)))]
      [else (append
             (list (format "For want of a ~a the ~a was lost." (list-ref lst index) (list-ref lst (add1 index))))
             (verse (add1 index)))]))
  (cond
    [(empty? lst) '()]
    [else (verse 0)]))
  • With a default argument, but we check if lst is empty for each value of index and this exposes weird usage possibilities:
(define (recite lst [index 0])
  (cond
    [(empty? lst) '()]
    [(= index (sub1 (length lst))) (list (format "And all for the want of a ~a." (list-ref lst 0)))]
    [else (append
           (list (format "For want of a ~a the ~a was lost." (list-ref lst index) (list-ref lst (add1 index))))
           (recite lst (add1 index)))]))

@github-actions
Copy link

Hello. Thanks for opening a PR on Exercism. We are currently in a phase of our journey where we have paused community contributions to allow us to take a breather and redesign our community model. You can learn more in this blog post. As such, all issues and PRs in this repository are being automatically closed.

That doesn't mean we're not interested in your ideas, or that if you're stuck on something we don't want to help. The best place to discuss things is with our community on the Exercism Community Forum. You can use this link to copy this into a new topic there.


Note: If this PR has been pre-approved, please link back to this PR on the forum thread and a maintainer or staff member will reopen it.

@github-actions github-actions bot closed this Oct 29, 2023
@Adrien-LUDWIG
Copy link
Contributor Author

@BNAndras
Just to make sure you see the PR. 😉

@BNAndras
Copy link
Member

Thanks again. I'll review tonight.

@Adrien-LUDWIG Adrien-LUDWIG changed the title Add proverb exercise Add proverb exercise (#297) Oct 29, 2023
@BNAndras
Copy link
Member

#2 is idiomatic since it signals a helper function is being used and it's defined before possibly being used. It's also an error to not have an expression after each internal define form so having the defines at the beginning makes that error a bit less likely to happen.

We could also forgo the helper function possibly by using a for/list form to manage the current index. The for/list form builds a list from the result from each iteration which is what we want.

@github-actions
Copy link

Hello. Thanks for opening a PR on Exercism. We are currently in a phase of our journey where we have paused community contributions to allow us to take a breather and redesign our community model. You can learn more in this blog post. As such, all issues and PRs in this repository are being automatically closed.

That doesn't mean we're not interested in your ideas, or that if you're stuck on something we don't want to help. The best place to discuss things is with our community on the Exercism Community Forum. You can use this link to copy this into a new topic there.


Note: If this PR has been pre-approved, please link back to this PR on the forum thread and a maintainer or staff member will reopen it.

@github-actions github-actions bot closed this Oct 30, 2023
@BNAndras BNAndras reopened this Oct 30, 2023
@BNAndras
Copy link
Member

There we go. The pause community contributions CI was misbehaving earlier.

@Adrien-LUDWIG
Copy link
Contributor Author

Sorry for the force push, I mixed up proverb.rkt and example.rkt.

Indeed, for/list is very useful. You don't even need for an index.
What do you think of the result?

@BNAndras
Copy link
Member

Looks good to me. Let me know if you're not getting Hacktoberfest credit for the PRs.

@BNAndras BNAndras merged commit d353729 into exercism:main Oct 30, 2023
7 checks passed
@Adrien-LUDWIG Adrien-LUDWIG deleted the add-proverb branch October 30, 2023 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
x:module/practice-exercise Work on Practice Exercises
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants