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

HW: Week 1 #4

Open
wants to merge 4 commits into
base: ogz
Choose a base branch
from
Open

HW: Week 1 #4

wants to merge 4 commits into from

Conversation

WizardOfOgz
Copy link
Collaborator

No description provided.

(hanoi4 (n - m) a c b d) ++
(hanoi m a b d) ++
(hanoi4 (n - m) c b a d)
where m = floor (sqrt (fromIntegral (n * 2)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You found a minimal solution! That is awesome. I looked at it for quite a while and gave up. I also love that you used hanoi in this solution.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adkron I found out that it's not actually an optimal solution (technically no solution has been proven to be optimal, but many believe that this is optimal). I didn't properly form a recurrence relation when analyzing the algorithm...mostly because it was a complex case and my math skills are weak. However, I got close enough to satisfy the specs, which made me happy.

hanoi4 _ _ _ _ _ = []
hanoi4 2 a b c _ = [(a, c), (a, b), (c, b)]
hanoi4 1 a b _ _ = [(a, b)]
hanoi4 0 _ _ _ _ = []
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the hanoi4 above this point can just delegate to hanoi since the outcomes are the same. I spent some time looking at this on mine and trying to see how I could utilize hanoi and noticed those outputs are the same.

Copy link
Collaborator Author

@WizardOfOgz WizardOfOgz Nov 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your comment made me realize that the case hanoi4 2 a b c _ on line 42 can be omitted completely (which I just tested)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I didn't think about that.

toDigitsRev x = [x]
toDigitsRev x
| x > 0 = (mod x 10) : (toDigitsRev $ div x 10)
| otherwise = []
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if having the thing that happens the most on top of the where clauses have a performance gain? I would think so since the compiler doesn't know which will happen more often. I like that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, yes, I believe there is a slight performance gain when placing the most frequently matched patterns as near to the top as possible. I just wanted to put the "default" case last. Even if the compiler was unable to optimize for your use cases, if the function was being called frequently (enough to warrant optimization) then you would hopefully benefit from processor branch prediction.

I found these relevant posts
https://elixirforum.com/t/does-placing-the-most-likely-matched-function-heads-first-offer-any-benefit/17428
https://stackoverflow.com/questions/48572426/performance-of-exhaustive-haskell-pattern-matching

@@ -0,0 +1 @@
:set -isrc/Cis194/Hw/Week1.hs -itest/Cis194/Hw
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know you could do this. That is awesome!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is another thing I tried using before I was able to run the spec file. IIRC the .ghci file has no impact when executing stack runghc.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is too bad. I wonder if stack has something like that. Like can I set a default command? I'll do some digging when I get a moment.

@adkron
Copy link
Collaborator

adkron commented Nov 20, 2018

I think the code looks great. I can't wait to get started on week 2 homework. Keep up the good work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants