You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 s is just the words function acting on s.
  • Next, map read (words s) is the same as (map read . words) s—we're composing map read with words to 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 composing sum with the previous combined function.
  • Finally, show (sum (map read (words s))) becomes (show . sum . map read . words) s—we add show to 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 press Ctrl+D (Linux/macOS) or Ctrl+Z (Windows) to signal end of input. The program will print the sum immediately.
  • Pipe input from another command: Use echo to 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 (like 1 2 on one line, or 1 on one line and 2 on 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 00:27:42