Skip to content

Comment on The Type of Sprintfparent

Comments

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.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.