Hacker News new | ask | show | jobs
by jznsis 758 days ago
I'm not that familiar with syntax of functional programming languages.

Isn't Int -> String -> String a shorthand for Int -> (String -> String)?

Thus I would have assumed the first argument to the result of sprintf "%s %d" would be an integer, then a string and the final result would be a string.

But the invocation would be sprintf "%s %d" "hello" 42, wouldn't it?

1 comments

> Isn't Int -> String -> String a shorthand for Int -> (String -> String)?

Yes. Though I find it more convenient think of it as a function with multiple arguments and not a function that returns another function.

> Thus I would have assumed the first argument to the result of sprintf "%s %d" would be an integer, then a string and the final result would be a string.

Why assume that? (Not rhetorical.) Wouldn't that be like if in C, you had to write the arguments in reverse order of the format string, like this?

  printf("%s %d!\n", 5, "hi");
I seem to be missing something, thus thank you for taking your time

> Wouldn't that be like if in C, you had to write the arguments in reverse order of the format string, like this?

Exactly and that's not what we want

Let's go through this step by step:

sprintf "%s %d" returns a function, taking a string and returning a function taking an integer and returning a string, right?

sprintf "%s %d" "hello" returns a function taking an integer, returning a string, right?

sprintf "%s %d" "hello" 42 returns a string, right?

Thus my intuition of the return type of sprintf "%s %d" would be string -> (int -> string) not int -> (string -> string)

Your intuition is correct. I think you accidentally swapped %s and %d in one of your previous posts, leading to the confusion.

The "backwards" in the article is referring to the way the list is processed by the recursive function. The list and the resulting type are in the correct/intuitive order. I also found this sentence confusing.

By the way, thinking of a function of type a->b->c as a->(b->c) is technically correct and useful in certain cases, for example, when you are currying. But in most cases you can just read this as a function taking two arguments of types a and b, returning something of type c.