Live data from Hacker News

Translating math into code with examples in Java, Racket, Haskell, Python (2011)

matt.might.net

41–50 of 95 posts

Re: Translating math into code with examples in Java, Racket, Haskell, Python (2011)

#41

Despite the title, this article does a real disservice to Python. For the purpose of constructing a generator, the author wrote: > In an object-oriented setting like Python or Java, streams can be constructed from an interface Yikes. itertools was introduced in, let's see... 2003?

Python has both generator functions and generator expressions. A generator function is like this:

    def int_generator():
        i = 0
        while True:
           yield i
           i += 1
A generator expression looks like a list comprehension (a Python construct based on the set-builder notation in Math) and is used in situations where you don't really need to materialize the list - for example when you are just iterating over it in a `for` loop:

    for n, square in ((i, i * i) for i in int_generator()):
        print(f"{n}² = {square}")
        time.sleep(1)

Re: Translating math into code with examples in Java, Racket, Haskell, Python (2011)

#42
post #4

I dream of material that would explain some advanced (for a regular person understanding of the word) math concepts using Python. Maybe it's because I've programmed more in my life than did math, but the "language" we use to write it is absolutely crazy: one letter variables everywhere, no structure, thousands of custom notations etc. I'm not criticising it, I realise it exists this way for a reason and if I wasn't s…

Gerry Sussman had the same idea and wrote a paper on it:

https://www.researchgate.net/publication/37597511_The_Role_o...

Re: Translating math into code with examples in Java, Racket, Haskell, Python (2011)

#43
post #28

> Mathematics is a purely functional language. It is not. E.g. x ∈ ℕ ∧ x > 2 ∧ x This is just one of the many mathy myths that plague the FP community. Yes, there are some similarities between FP and mathematical notation, but FP and imperative are much closer to one another than either is to mathematics.

Not to mention that whole fields of mathematics deal with numerical algorithms: Computational algebraic geometry, number theory, numerical mathematics etc.

Re: Translating math into code with examples in Java, Racket, Haskell, Python (2011)

#44
post #4

I dream of material that would explain some advanced (for a regular person understanding of the word) math concepts using Python. Maybe it's because I've programmed more in my life than did math, but the "language" we use to write it is absolutely crazy: one letter variables everywhere, no structure, thousands of custom notations etc. I'm not criticising it, I realise it exists this way for a reason and if I wasn't s…

I suspect what's jarring is the difference between programming in an imperative sense, where you write down steps to be taken to get a computation, and equations, like declarative programming. I once dated, in math grad school, a hardcore differential equations student, and she took Java, which she thought so very different than a world of equations. Made sense, I thought, having been exposed to imperative and declar…

Math has a syntax to write imperative algorithms in too. Came up in the Discrete Math course I took, for things like finding GCD.

Re: Translating math into code with examples in Java, Racket, Haskell, Python (2011)

#45
post #4

I dream of material that would explain some advanced (for a regular person understanding of the word) math concepts using Python. Maybe it's because I've programmed more in my life than did math, but the "language" we use to write it is absolutely crazy: one letter variables everywhere, no structure, thousands of custom notations etc. I'm not criticising it, I realise it exists this way for a reason and if I wasn't s…

I've come to the conclusion that my brain's "algorithm"y, rather than "equation"y. My scores on (especially) spatial reasoning tests are excellent so one would think I'd have a leg up in mathematics, but trying to read typical "mathy" (equation-heavy) writing makes me feel the way I assume dyslexics do, even when it's something I'm pretty familiar with.

Algorithms, though? No problem. Programming in typical Algol-family languages & similar came easily to me. The "tough" early topics? Pointers, recursion? Not even a bump in the road, totally natural. To this day, though, the ones that look more like mathematical writing (Haskell) are really hard to read, let alone write. I can get all the concepts at play in a piece of Haskell and still not be able to keep the syntax or meaning of the code itself straight. When I do want or have to read mathematics of any complexity whatsoever, I only make headway by converting it to something more algorithmic as I go ("OK, so what does this term or whatever do to any other values passing through, here?")

Re: Translating math into code with examples in Java, Racket, Haskell, Python (2011)

#46
post #4

I dream of material that would explain some advanced (for a regular person understanding of the word) math concepts using Python. Maybe it's because I've programmed more in my life than did math, but the "language" we use to write it is absolutely crazy: one letter variables everywhere, no structure, thousands of custom notations etc. I'm not criticising it, I realise it exists this way for a reason and if I wasn't s…

Check SICM.

https://mitpress.mit.edu/sites/default/files/titles/content/...

Re: Translating math into code with examples in Java, Racket, Haskell, Python (2011)

#47
post #4

I dream of material that would explain some advanced (for a regular person understanding of the word) math concepts using Python. Maybe it's because I've programmed more in my life than did math, but the "language" we use to write it is absolutely crazy: one letter variables everywhere, no structure, thousands of custom notations etc. I'm not criticising it, I realise it exists this way for a reason and if I wasn't s…

There's a few books around for this, first is Jeremy Kun's book https://pimbook.org/ and is probably exactly what you're looking for: math from a programmer's perspective. There's also Discrete Math w/Functional programming https://cs.wheaton.edu/~tvandrun/dmfp/ in which most chapters translate theorems into algorithms or turn sets into types. Sussman also has two books translating Lagrange equations and differential eq into Scheme but both assume an undergrad physics background. Brown has a course in linear algebra using Python http://cs.brown.edu/courses/cs053/current/lectures.htm but you can do it with any math language library that builds matrices or write your own naive implementation as you go.

Of course the best way to do this would be to try and model the notation yourself (if possible) into function specs or algorithms, as you go through various math books. Then you'd really understand the notation even if you failed to implement it. You could try Pyret to do this https://papl.cs.brown.edu/2018/func-as-data.html#%28part._.A...

Re: Translating math into code with examples in Java, Racket, Haskell, Python (2011)

#48

Nice article, I was wondering why Julia wasn't featured considering its support for math syntax. I wondered whether this could have been an old article, but there is no date in the article. I checked the RSS feed and couldn't find the article in it. I cross-checked the oldest article in the feed with the index of articles on the site, this article seems to be written before Dec 2014. This makes sense for why Julia is…

Don't forget the Internet Archive!

The first version is from Nov. 14: https://web.archive.org/web/20111114185849/http://matt.might... The article index from Nov. 8 does not include the article: https://web.archive.org/web/20111108003034/http://matt.might...

So we can say with confidence that this should have a [2011] tag and was published sometime Nov 8 - Nov 14 in 2011.

Re: Translating math into code with examples in Java, Racket, Haskell, Python (2011)

#50
post #28

> Mathematics is a purely functional language. It is not. E.g. x ∈ ℕ ∧ x > 2 ∧ x This is just one of the many mathy myths that plague the FP community. Yes, there are some similarities between FP and mathematical notation, but FP and imperative are much closer to one another than either is to mathematics.

I don't know much about this area. What about "x ∈ ℕ ∧ x > 2 ∧ x < 5 ∧ x % 2 = 0 implies x = 4." can't be expressed in common languages? Don't lots of solver systems have support for sets, and deriving facts from statements about them?
Post reply on HN