Consider the following Haskell definitions, taken from this excellent Haskell video on YouTube:
import Data.List
greeting = "Hello"
swedish = intersperse 'f'
very f x = f (f (f x))
If we load them into GHCi, we see the following results:
ghci> swedish greeting
"Hfeflflfo"
ghci> very swedish greeting
"Hfffffffeffffffflffffffflfffffffo"
ghci> very very swedish greeting
"Hffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
fffffffffffffffffffffffffffff... (536,870,913 chars total)
The first two outputs I understand perfectly. A swedish greeting
comes out interspersed with f
s, and a very swedish greeting
is just a swedish (swedish (swedish greeting))
, which comes out triply interspersed.
But what exactly is happening in the third input line? My (rather incomplete) understanding of Haskell syntax says that a space-separated sequence of expressions is interpreted as a function call, where the first expression is the function and the rest are the arguments. In that case, how is the outermost very
being called with three arguments (very
, swedish
, and greeting
) when it's only defined to accept two?
If it helps, it appears that a very very swedish greeting
is equivalent to a swedish $ swedish $ swedish $ swedish $ ... (27 layers of swedish) ... $ swedish $ swedish greeting
.
The formal Swedish “Hello” is simply Hej!, which can sometimes be confusing because it sounds just like the informal English “Hey!” Saying it twice: Hej, hej! makes it more informal, though this greeting is often used as a form of “Goodbye” as well. Hejsan, hallå, tjena/tjenare are also informal ways to say “hello”.
When you get to work, you can use a Hej, hej (“Hello, hello” ) if you don't work in a more formal workplace like a bank. If you work in a more formal workplace, it might be better to say Hej (“Hello”), God morgon (“Good morning”), God dag (“Good day”), or God eftermiddag (“Good afternoon”).
Hej, pronounced like English 'hey', is the Swedish word for 'hello' or 'hi', an alternative for hallå (hello) or goddag (good day). The first thing you need to know is that hej is the most common and safest of the three to use as a non-native speaker.
You said:
My (rather incomplete) understanding of Haskell syntax says that a space-separated sequence of expressions is interpreted as a function call, where the first expression is the function and the rest are the arguments.
You are correct that this is not a complete understanding of what is actually going on. From your example:
very very swedish greeting
This is the same as:
((very very) swedish) greeting
This is because function application is left associative. Furthermore, every function in Haskell takes one input and returns one result. What you think of as functions accepting multiple inputs are, in reality, functions that accept a single input and return a function as their result.
This also explains why arrows (->) delimit function inputs and results in function types. Consider the type of ++
:
(++) :: [a] -> [a] -> [a]
This is the same as:
(++) :: [a] -> ([a] -> [a])
You may think of the ++
operator as taking two lists and returning a list, but in reality it is a function of one input (a list) that returns a function of one input (another list) that returns a list.
Taken all together, you can hopefully see that very very
is a valid expression by itself (and happens to have the same type as very
).
very very x
is equivalent to:
very (very (very x))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With