Giter VIP home page Giter VIP logo

haskeuler's Introduction

Project Euler in Haskell

Hello there, surfer of the interwebs! This is a log of my progress learning the programming language Haskell by solving the theoretical programming challenges found on Project Euler. Iā€™m still working on making my code more legible and efficient, the latter of which is not Haskellā€™s greatest strength, though itā€™s astoundingly efficient for its level of abstraction (praise be to Simon Peyton Jones & Jan-Willem Maessen et al.).

Have fun reading! šŸ˜Š

Goals

Major

  • Get comfortable with standard library. Pretty much the first step when learning any language is getting used to the nitty-gritty; learning the principles and idioms follows.
  • Get used to Hindleyā€“Milner type system, especially the type class hierarchy (which is nearly as contrived as the class hierarchy in OO languages, IMO, though I might change my mind once I get used to it).
  • Try to avoid the accumulator pattern, at least where I donā€™t specifically need it for tail recursion. Having two versions of every function makes code much less readable, and I donā€™t really feel like using a where clause to inline a multi-line function definition.

Minor

  • Move utility functions to external modules. Iā€™ve begun reusing certain functions (such as divBy and prime) throughout Project Euler, so this would reduce duplications and make code more readable (if spread out).
  • Remove commented sections. I started work on some pretty cool functions before realizing I didnā€™t actually need them for the task at hand. However, Iā€™m sure theyā€™d be useful for some use cases, and I couldnā€™t find anything similar on Hoogle. These will remain available in the commit log.

At some point

  • Try out Vim/Emacs. Iā€™ve been using Sublime Text with the SublimeHaskell and Hoogle plugins, but I could probably benefit from a tighter and more consistent set of keyboard shortcuts, a simpler API, and the comfort of knowing Iā€™m supporting open source. Might try Evil Mode, Spacemacs, or some other bastard child of the two major CLI editors.

Progress

  1. ā€œMultiples of 3 and 5ā€
  • Efficient? Runs in about .02 seconds, according to the Unix time utility. Thatā€™s pretty efficient as far as Iā€™m concerned. (And yes, I watched out for running the same program a few times in a row. Haskell is surprisingly well-optimized by the CPU cache, from what I can tell.)
  • Simple? Just two lines of definitions, taking advantage of list comprehensions and any :: (a -> Bool) -> [a] -> Bool. I probably couldnā€™t get better than this without pulling out the only function definition in the entire program.
  • Learned? Not all that much. Mainly got used to the type systemā€”specifically, the nature of mod :: Integral a => a -> a -> a.
  1. ā€œEven Fibonacci numbersā€
  • Efficient? .03 seconds.
  • Simple? Pretty simple at this point, though I wish there were some way to collapse the two function definitions required to make use of an accumulator parameter into one more compact definition.
  • Notes: Started out as somewhat of a cheat, where I found out how many even Fibonacci numbers were less than four million and hard-coded that value into the program by feeding it to take :: Int -> [a] -> [a]. This was because filter :: (a -> Bool) -> [a] -> [a] has no facilities to avoid enumerating the entire list, even when the given function is (< 4000000), since the type system canā€™t prove that the sequence is ever-increasing and therefore can never assume that the rest of the infinite list need not be checked.
  • Learned? I made stop :: (a -> Bool) -> [a] -> [a] in response; it works recursively to move items over one by one until an item matches the stop-test. Fairly proud of this solution, though I wonder whether I couldā€™ve used a function that was already somewhere in the standard library. On another note, this was the only problem for which I consulted the Internet, but this taught me about zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] as it can be used to define fibs. Not sure whether I could rewrite that definition off the top of my head; I should probably get used to defining lists in terms of initial items and how the following terms relate to them, as I donā€™t want to move past this without ensuring that I know what I did.

haskeuler's People

Contributors

ron-wolf avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.