Hacker News new | past | comments | ask | show | jobs | submit login

> Notice how state was introduced? It made the code easier to read.

That quote shows that the author kinda misses the point here.

    type FibPair = (Int, Int)

    fibSeed :: FibPair
    fibSeed = (0, 1)

    fibNext :: FibPair -> FibPair
    fibNext (p, v) = (v, v + p)

    fibList :: [FibPair]
    fibList = iterate fibNext fibSeed
The above code does exactly the same as his Fibonacci example, and it's written in pure Haskell. I'd argue the above is way more readable.

I get the following output:

    0 1 1 2 3 5 8 13 21 34
With the following main function:

    main :: IO ()
    main = putStrLn . unwords . map (show . fst) . take 10 $ fibList
Which just takes the first element of each generated tuple, maps it to the string representation, and then adds a space between each number.



I translated your example to Kotlin just for fun, as close as possible. Probably not idiomatic Kotlin, but pretty close, if you ask me.

    typealias FibPair = Pair<Int, Int>
    
    val fibSeed = FibPair(0, 1)
    
    fun fibNext(pv: FibPair) = pv.let { (p, v) -> FibPair(v, v + p) }
    
    fun fibList(): Sequence<FibPair> = generateSequence(fibSeed, ::fibNext)
    
    fun main() {
        fibList().take(10).forEach { (p, _) -> print("$p ") }
    }
Honestly I like Kotlin's Sequence abstraction much more than Java streams. It's extremely simple to implement and understand contrary to streams.


Right, his Fibonacci class is completely immutable, i.e. it's purely functional.

He's confusing different usages of the word "state" - exhortations to avoid state are almost always about mutable state, but he doesn't appear to recognize that distinction.


Tell that to the person at work who recently came back from a FP workshop and has declared war on all state...

Developers to tend to go from extremes.


The question is what he means by "all state".

If we take the OP post as definitional, then eliminating state means eliminating compound data structures, because that's the usage of "state" implied by that Fibonacci example.

Hopefully the person at your work has something less extreme in mind. Ask him to explain what he means by "state".


That code is not at all readable, even knowing Haskell.

Even the recursive python version with memoization is a bit difficult to read, but least it has the recurrence relation in it.

    vals = {}

    def fib(n):
      if n < 2:
        return n
      if n in vals.keys():
        return vals[n]
      val = fib(n-1) + fib(n-2)
      vals[n] = val
      return val


Your code is doing something pretty different from the examples above. You're just calculating the nth fibonacci number, not creating an infinite fibonacci _sequence_. Eliding the explicit recursive structure is the point of the exercise!

Admittedly Python's facilities for doing that with some mutable state are pretty nice:

  from itertools import islice

  def fib():
      a, b = 0, 1
      while True:
          yield a
          a, b = b, a + b

  list(islice(fib(), 10)) == [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]


I wasn't speaking to the goal of generating a sequence that can be lazily evaluated.

I was speaking to readability.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: