Live data from Hacker News

Destroy All Ifs – A Perspective from Functional Programming

degoes.net

221–226 of 226 posts

Re: Destroy All Ifs – A Perspective from Functional Programming

#221
post #202
post #193

Earlier quoted context omitted.

> does Haskell even have a stack? Yes

While this is true, the contents of the STG stack aren't necessarily obviously related to the conceptual "call stack", right?

Well it's case that pushes onto the stack rather than (syntactic) function call but if you're willing to be generous with what you consider "call" then, yes, they're related.

Re: Destroy All Ifs – A Perspective from Functional Programming

#222
post #134

Earlier quoted context omitted.

Thats why you use Lua, it lets you have multiple return values. So you can get a boolean back to let you know if the strings were the same, an int to know where they ceased matching and a boolean to let you know if they are case different. It's then up to the programmer to decide how much enlightenment they want. The destroy all IF reminds me of GOTO considered harmful of the 70's. There are other ways to fix the pro…

What's the difference between multiple return values and returning a tuple? (Apart from that languages with multiple return values tend to have some special syntax for binding only the first few members of the returned tuple?)

Apart from what you mention, it's often not possible to pass along all of the multiple return values as a single value.

Re: Destroy All Ifs – A Perspective from Functional Programming

#223
post #191

Earlier quoted context omitted.

The point of tail recursion is using constant space, not constant stack space (does Haskell even have a stack?) Anyways, the Haskell spec allows foldl' to use linear space just like its lazier counterparts. The fact that it uses constant space is an implementation detail of GHC. Reference: https://github.com/quchen/articles/blob/master/fbut.md#seq-d... Structural recursion always terminates in SML. Supporting infinit…

Tail recursion can't use constant space if it's strictly generating another data structure of the same size. That doesn't even make sense. Interesting fact about foldl'. Regardless, in practice it is strict and tail recursive. As I mentioned earlier, this does not mean the same thing as constant space unless the reduction function returns a fixed size result. Yes, you can guarantee that a linked list in Java is finit…

> Yes, you can guarantee that a linked list in Java is finite because Java does not support codata.

What about another thread running that keeps generating pieces to the end of the linked list? (No problem, with mutation.)

Re: Destroy All Ifs – A Perspective from Functional Programming

#224
post #145

Earlier quoted context omitted.

Interesting. I assume they allow the special cases of tail recursion introduced by 'while', 'for' and similar constructs?

goto is banned; loops must be statically bounded.

Thanks for the information!

Re: Destroy All Ifs – A Perspective from Functional Programming

#225
post #223
post #191

Earlier quoted context omitted.

Tail recursion can't use constant space if it's strictly generating another data structure of the same size. That doesn't even make sense. Interesting fact about foldl'. Regardless, in practice it is strict and tail recursive. As I mentioned earlier, this does not mean the same thing as constant space unless the reduction function returns a fixed size result. Yes, you can guarantee that a linked list in Java is finit…

> Yes, you can guarantee that a linked list in Java is finite because Java does not support codata. What about another thread running that keeps generating pieces to the end of the linked list? (No problem, with mutation.)

To prevent these and similar "what abouts", here's an implementation of a guaranteed finite linked list in Java.

    class LinkedList {
      public final T value;
      public final LinkedList next;
      public LinkedList(T value, LinkedList next) {
        this.value = value;
        this.next = next;
      }
    }
Here's how you construct it:

    LinkedList myList =
      new LinkedList("Hello",
        new LinkedList("World", null));
Here's how you iterate over it in constant space:

    while (myList != null) {
      System.out.println(myList.value);
      myList = myList.next;
    }

Re: Destroy All Ifs – A Perspective from Functional Programming

#226
post #211

Earlier quoted context omitted.

They don't have `other primitives': they have function calls. Most languages have function calls these days.

Yes, I read LTUI and LTUD. But in most languages, function calls and loops don't have the same semantics. I'll call that a different loop primitive.

For C, this seems to be implementation defined.

(At least for C as encountered in the wild, I don't know about C the standard.)

Most modern C compilers support tail call optimization.

I don't know about `most languages'. Eg I know Java on the JVM doesn't do tail call optimization. Lots of languages probably do not require TCO of their implementations, though.

Post reply on HN