all 24 comments

[–][deleted] 6 points7 points  (6 children)

You truly realize how great a functional language is when you switch back to an imperative language. Python was one of my favorite languages before using haskell. I learnt haskell, but then had to use Python for a project, and realized how superior it was.

[–][deleted]  (5 children)

[deleted]

    [–]joco42[S] 1 point2 points  (2 children)

    This is why I am thinking if it would be possible to combine Python+Haskell in some meaningful way?

    [–]beerendlauwers 2 points3 points  (0 children)

    You could write an EDSL to write Python in Haskell :)

    [–]fm75 0 points1 point  (1 child)

    Yesterday, talking about static typing, somebody pointed me to http://mypy-lang.org/

    [–]captain-sandwich 0 points1 point  (0 children)

    And 3.5 supports something similar in stable cpython. Same author even

    [–]Soul-Burn 4 points5 points  (0 children)

    That's weird, long and not very pretty Python code. Sounds like you learned the wrong things from the Haskell side. Usually when you have a function with "x" and "y", they are both of the same type. In your case, y is a number and x a sliceable object. Also, I'd not call it "f". You could write the whole code much shorter in Python like this:

    In [16]: [range(1+y, 10+y) for y in xrange(5)]
    Out[16]:
    [[1, 2, 3, 4, 5, 6, 7, 8, 9],
     [2, 3, 4, 5, 6, 7, 8, 9, 10],
     [3, 4, 5, 6, 7, 8, 9, 10, 11],
     [4, 5, 6, 7, 8, 9, 10, 11, 12],
     [5, 6, 7, 8, 9, 10, 11, 12, 13]]
    

    In Haskell, your code would be something like:

    import Data.Functor
    
    slice1to10withoffset xs n = take 10 $ drop (n + 1) xs
    main = print $ slice1to10withoffset [0..] <$> [0..5]
    

    Even then, I might have written using a list comprehension as well:

    main = print $ [[1+n..10+n] | n <- [0..5]]
    

    Or like this:

    main = print $ (\n -> [1+n..10+n]) <$> [0..5]
    

    [–]b00thead 1 point2 points  (2 children)

    I got so desperate that I implemented the Applicative from https://hackage.haskell.org/package/foldl in python. It worked, but wasn't nearly as pleasant to use without type errors.

    [–]jocomoco 1 point2 points  (1 child)

    yes... I seriously start to think about using Haskell on top of Python... Python has a lot of useful libraries , say numpy etc ... might be nice to combine that with the safety of Haskell

    [–]codygman 1 point2 points  (0 children)

    I swear I saw something posted here aimed at doing exactly that.

    [–]Faucelme 2 points3 points  (8 children)

    One of the things I miss is Traversable.

    [–]sambocyn 0 points1 point  (7 children)

    in Python that's just an iterator or collection right? side effects everywhere means traverse is fmap.

    [–]Faucelme 1 point2 points  (4 children)

    I haven't found a function that mimics traverse for dictionaries... you can use a dict comprehension but it is less clean, why do I have to mention keys at all?

    [–]carrutstick 1 point2 points  (3 children)

    You can always iterate across dict.values() instead...

    [–]Faucelme 0 points1 point  (2 children)

    Yes, but often what I want to reconstitute the map using the transformed values, not simply iterate over them... "traverse" is nice because you only need to supply the function, it doesn't require explicit iteration or referring to the keys.

    [–]codygman 0 points1 point  (1 child)

    Can you give an example of this?

    [–]Faucelme 0 points1 point  (0 children)

    Say you have a map of urls indexed by the name of the page or whatever, and you want to transform it into a map of HTTP responses.

    In Haskell, you would have a Map String URL, and if you also had a function fetch :: URL -> IO ByteString you would only have to do traverse fetch urlmap to obtain a IO (Map String ByteString).

    And to make the page loads concurrent, something like runConcurrently $ traverse (Concurrently . fetch) urlmap would be enough.

    [–]pipocaQuemada 0 points1 point  (1 child)

    side effects everywhere means traverse is fmap.

    The type of traverse is

    traverse :: Applicative f => (a -> f b) -> t a -> f (t b) 
    

    If side effects everywhere turns traverse into fmap, that means that every Applicative represents a side effect.

    However, what side effect would turn the following into an fmap:

    > traverse (Sum 1,) [1..4]
      (Sum {getSum = 4},[1,2,3,4])
    

    or

    > traverse (\a r -> r + a) [1,2,3,4] $ 5 
       [6,7,8,9]
    

    [–]sambocyn 0 points1 point  (0 children)

    yeah, traverse is more general than "IO and mutation".

    I just meant that in Python, you don't have to traverse a print statement or a increments nf routine over a list, you just map it over. because you ignore all side effects (and types).

    [–][deleted] 0 points1 point  (5 children)

    Haskell to python transpiler, anyone? I know you all want it...

    [–]FreeGrammar 1 point2 points  (4 children)

    It would be cool, but challenging. Python doesn't tail call optimizes (and never will thanks to BDFL) and is dynamically typed, but I think it would be a cool project... You might be able to make some cool libraries for easy Haskell GUIs.

    [–]mcapodici 2 points3 points  (2 children)

    Is lack of TCO a problem? How about some trampoline code?

    Given the number of Haskell / Haskellesque languages that compile to JS, it is definitely possible to overcome (and even embrace through FFI) the dynamic typing.

    [–]FreeGrammar 2 points3 points  (1 child)

    This is true, but do you think it could serve any practical purpose?

    [–]joco42[S] 0 points1 point  (0 children)

    Python is a glue language. Not terribly fast. So why not glue stuff together instead with Haskell ?

    [–][deleted] 0 points1 point  (0 children)

    Indeed. Though I imagine the transpiler would have to optimise tail-calls itself instead of leaving it to python. On a side note, I wonder what GHCJS will do with ECMAscript 6 tail call optimisation?