Haskell入门疑问:$运算符转函数组合的分步解析及interact方案的参数运行方法
Hey there! Let's break this down step by step—you're already making great progress with Haskell, so let's clear up those two confusion points for you.
1. Step-by-step conversion from $ to function composition (.)
First, let's recap the expression you understood:
show $ sum $ map read $ words "1 2"
Remember that $ is the application operator—it's just a way to avoid writing parentheses. This line is exactly equivalent to:
show (sum (map read (words "1 2")))
Our goal is to turn this into a reusable function that takes any input string (like "1 2") and returns the formatted sum. Here's how we do the conversion step by step:
Step 1: Remove the fixed input
Instead of hardcoding "1 2", we want a function that takes a string s and does the same logic. Start with:
show (sum (map read (words s)))
Step 2: Replace nested parentheses with function composition
The function composition operator . works like this: for any two functions f and g, f . g is a new function that does f(g(x)) when given x. Let's apply this from the innermost layer out:
- First,
words sis just thewordsfunction acting ons. - Next,
map read (words s)is the same as(map read . words) s—we're composingmap readwithwordsto make a function that takes a string, splits it into words, then converts each word to a number. - Then,
sum (map read (words s))becomes(sum . map read . words) s—now we're composingsumwith the previous combined function. - Finally,
show (sum (map read (words s)))becomes(show . sum . map read . words) s—we addshowto the front of the composition to turn the sum into a string.
Step 3: Pass the composed function to interact
The interact function expects a String -> String function: it reads all standard input as a single string, passes it to your function, then prints the resulting string. Since show . sum . map read . words is exactly that type of function, we can write:
main = interact $ show . sum . map read . words
The final $ here is just applying the composed function to interact—it's the same as interact (show . sum . map read . words).
2. Running the interact version from the command line
Once you've written the code into a file (say, SolveMeFirst.hs), here's how to run it:
Step 1: Compile the program
Use GHC to compile the Haskell file into an executable:
ghc SolveMeFirst.hs
This will generate an executable file: SolveMeFirst (Linux/macOS) or SolveMeFirst.exe (Windows).
Step 2: Run with input
You have a few options for passing input:
- Direct interactive input: Run the executable, type your input (e.g.,
1 2), then pressCtrl+D(Linux/macOS) orCtrl+Z(Windows) to signal end of input. The program will print the sum immediately. - Pipe input from another command: Use
echoto send input directly to the program:# Linux/macOS echo "1 2" | ./SolveMeFirst # Windows (Command Prompt/PowerShell) echo "1 2" | SolveMeFirst.exe - Read input from a file: If you have a text file (e.g.,
input.txt) with your input (like1 2on one line, or1on one line and2on another), run:# Linux/macOS ./SolveMeFirst < input.txt # Windows SolveMeFirst.exe < input.txt
A quick note: words ignores all whitespace (including newlines), so inputting two separate lines (like 1 followed by 2) will work exactly the same as inputting them on one line—perfect for matching the original readLn behavior!
内容的提问来源于stack exchange,提问作者Pastilhas

