Thanks I will look at that too. Clever and ignorant are synonyms in my book, and I am ignorant here. In this case IO and memory was what was interesting in the article.
I'm being way to clever... I read your article when it was originally posted, but hadn't realized that you're quoting from it! My bad...
If you'll humor me, I'd love to take another shot, but from the more interesting direction!
Adding a type might clear stuff up a bit:
-- hs
pure (n, guard (factor /= n) $> factor) :: IO (Int, Maybe Int)
# py
print((n, if factor == n then factor else None))
So without the for loop, there's no list to comprehend. Adding the list back gives something more equivalent to a standard for loop in Python:
-- hs
for numbers $ \n ->
pure (n, guard (factor /= n) $> factor) :: IO [(Int, Maybe Int)]
# py
for n in numbers:
print((n, if factor == n then factor else None))
Which could also be written as a list comprehension in either language:
-- hs
[pure (n, guard (factor /= n) $> factor) | n <- numbers] :: IO [(Int, Maybe Int)]
# py
[print((n, if factor == n then factor else None)) for n in numbers]
Note that they behave a bit differently though, and it's the reason I haven't included `return` in the python line (what does `print` return?). Python will loop through the list and actually run the `print` function on each element, while Haskell will loop through and collect all the `IO` into one function to run later. Although it's starting to get pretty un-Pythonic, you can hack the behavior into Python with something like:
# py
return lambda: [f for f in (print((n, if factor == n then factor else None)) for n in numbers)]
Which would need to be run using something unholy like:
Comments
Yeah, but what is worst? I tried to understand the Haskell way in an article last week[0]
Which I think is more or less the same as this python line. The article does fancy stuff with memory caches which I believe is easy to do in python but I need to understand the Haskell code better.[0] Haskell: A Great Procedural Language https://entropicthoughts.com/haskell-procedural-programming#... https://news.ycombinator.com/item?id=42754098
Returns a tuple: Left side is n, right side is (Just factor) if factor is not n, or Nothing if it is.
You're being way to clever! The machinery from the article is only needed to deal with IO.
A more direct translation:
The full functions:Thanks I will look at that too. Clever and ignorant are synonyms in my book, and I am ignorant here. In this case IO and memory was what was interesting in the article.
I'm being way to clever... I read your article when it was originally posted, but hadn't realized that you're quoting from it! My bad...
If you'll humor me, I'd love to take another shot, but from the more interesting direction!
Adding a type might clear stuff up a bit:
So without the for loop, there's no list to comprehend. Adding the list back gives something more equivalent to a standard for loop in Python: Which could also be written as a list comprehension in either language: Note that they behave a bit differently though, and it's the reason I haven't included `return` in the python line (what does `print` return?). Python will loop through the list and actually run the `print` function on each element, while Haskell will loop through and collect all the `IO` into one function to run later. Although it's starting to get pretty un-Pythonic, you can hack the behavior into Python with something like: Which would need to be run using something unholy like: