Haskelling My Python
The article explains implementing Haskell's lazy infinite lists in Python using generators, covering recursive integer generation, Taylor series for \( e^x \), sine and cosine functions, and memoization for performance.
Read original articleThe article discusses the implementation of Haskell's lazy infinite lists in Python using generators. It begins with a simple generator for positive integers, demonstrating how to create it recursively. The author explains the integration of a Taylor series, showcasing how to define the exponential function \( e^x \) using its properties. The article also covers the evaluation of the Taylor series against Python's standard library function for \( e^x \), showing that the results are closely aligned. Additionally, the author introduces the sine and cosine functions through their integral relationships, providing Python implementations for both. To enhance performance, a memoization technique is suggested to address the inefficiencies of Python generators compared to Haskell's lists. The article concludes with a note on using Python's fractions module for rational number outputs.
- The article illustrates how to implement Haskell's lazy infinite lists in Python.
- It demonstrates the creation of a recursive generator for positive integers.
- The integration of Taylor series is explained, particularly for the exponential function \( e^x \).
- Sine and cosine functions are derived using their integral relationships.
- A memoization technique is recommended to improve the performance of Python generators.
Related
Ordinals aren't much worse than Quaternions
The article explores the computability of ordinals up to ε₀, comparing them to quaternions and complex numbers, and discusses their unique properties, Python implementations, and recursive nature in mathematics.
What is the longest known sequence that repeats in Pi? (homelab)
The article explores repeating sequences in Pi's decimal representation, detailing the transition from Python to C for efficiency, successful identification of sequences, and plans for further extensive searches.
A joke in approximating numbers raised to irrational powers
The blog post explores approximating irrational powers without calculators, emphasizing Padé approximations over Taylor Series. It provides rational functions for key mathematical functions and includes Python code for visualization.
Specializing Python with E-Graphs
A toy compiler for Python expressions uses the egglog library and e-graphs for optimization, enhancing numerical computations through stages like function decoration, expression extraction, and MLIR code generation.
Interesting Python features that I've come across in my career
Edward Li's blog post discusses 14 advanced Python features, including typing overloads, keyword-only arguments, future annotations, generics, and protocols, providing examples and resources to enhance programming skills.
- Several commenters express admiration for the use of generators, highlighting their memory efficiency and ability to create transformation pipelines.
- There are concerns about the readability and maintainability of the Python code compared to Haskell, with some suggesting that it can become difficult to debug.
- Some users question the practicality of the recursive function implementation, with one commenter expressing skepticism about its effectiveness.
- Discussions include references to alternative approaches and languages, such as F# and numpy, indicating a broader context of functional programming.
- A few comments critique Python's flexibility, suggesting it can lead to convoluted code that is hard to maintain.
I implemented the same thing myself in F#. [1]
[0]: https://citeseerx.ist.psu.edu/document?repid=rep1&type=pdf&d...
f = x -> raise if
x :: int => IndexError x
otherwise => ValueError x
Complete with pipelines, of course: "> {}: {}".format "Author" "stop using stale memes"
|> print
When working with other engineers, I’ve learned to be careful: sometimes it’s better to just materialize things into a list for clarity, even if it’s less “elegant” on paper.
There’s a real balance between cleverness and maintainability here.
Actually, it's even simpler than that: the $ operator is nothing but a function that applies its left argument to its right one! The full definition is
f $ x = f x
(plus a directive that sets its precedence and association) def ints():
yield 1
yield from map(lambda x: x + 1, ints())
Surely it would always yield a stream of `1`s? Seems very weird to my brain. "As simple as that" it is not!The reason this all works is that generators plus memoization is "just" an implementation of the lazy sequences that Haskell has built in.
Alternatives that aren't dumb:
for x in range(2**256): # not infinite but go ahead and run it to the end and get back to me
from itertools import repeat
for x, _ in enumerate(repeat(None)): # slightly more annoying but does work infinitely
Granted these aren't clever or non-performant enough to excite functional code fanboys.If you want to code it Haskell, have you considered using Haskell?
Related
Ordinals aren't much worse than Quaternions
The article explores the computability of ordinals up to ε₀, comparing them to quaternions and complex numbers, and discusses their unique properties, Python implementations, and recursive nature in mathematics.
What is the longest known sequence that repeats in Pi? (homelab)
The article explores repeating sequences in Pi's decimal representation, detailing the transition from Python to C for efficiency, successful identification of sequences, and plans for further extensive searches.
A joke in approximating numbers raised to irrational powers
The blog post explores approximating irrational powers without calculators, emphasizing Padé approximations over Taylor Series. It provides rational functions for key mathematical functions and includes Python code for visualization.
Specializing Python with E-Graphs
A toy compiler for Python expressions uses the egglog library and e-graphs for optimization, enhancing numerical computations through stages like function decoration, expression extraction, and MLIR code generation.
Interesting Python features that I've come across in my career
Edward Li's blog post discusses 14 advanced Python features, including typing overloads, keyword-only arguments, future annotations, generics, and protocols, providing examples and resources to enhance programming skills.