ruby while loop translated to haskell

后端 未结 2 1982
被撕碎了的回忆
被撕碎了的回忆 2021-01-20 18:42

I\'ve just started learning a bit of Haskell and functional programming, but I find it very difficult getting a hang of it :)

I am trying to translate a small piece

相关标签:
2条回答
  • 2021-01-20 19:12

    Try this:

    let results =  [(x,y) | x <- [1..1000], y <- [1..1000] ,1 + fac x == y*y]
                   where fac n = product [1..n]
    

    This is a list comprehension. More on that here.

    To map it to your Ruby code,

    1. The nested loops in m and n are replaced with x and y. Basically there is iteration over the values of x and y in the specified ranges (1 to 1000 inclusive in this case).
    2. The check at the end is your filter condition for getting Brown numbers.
    3. where allows us to create a helper function to calculate the factorial.

    Note that instead of a separate function, we could have computed the factorial in place, like so:

    (1 + product[1..x]) == y * y
    

    Ultimately, the (x,y) on the left side means that it returns a list of tuples (x,y) which are your Brown numbers.

    OK, this should work in your .hs file:

    results :: [(Integer, Integer)] --Use instead of `Int` to fix overflow issue
    results =  [(x,y) | x <- [1..1000], y <- [1..1000] , fac x == y*y]
            where fac n = product [1..n]
    
    0 讨论(0)
  • 2021-01-20 19:15

    To add to shree.pat18's answer, maybe an exercise you could try is to translate the Haskell solution back into Ruby. It should be possible, because Ruby has ranges, Enumerator::Lazy and Enumerable#flat_map. The following rewritten Haskell solution should perhaps help:

    import Data.List (concatMap)
    
    results :: [(Integer, Integer)]
    results = concatMap (\x -> concatMap (\y -> test x y) [1..1000]) [1..1000]
        where test x y = if fac x == y*y then [(x,y)] else []
              fac n = product [1..n]
    

    Note that Haskell concatMap is more or less the same as Ruby Enumerable#flat_map.

    0 讨论(0)
提交回复
热议问题