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

Common Lisp函数编译与测试问询:统计列表正整数个数

Alright, let's break down how to compile your Common Lisp code and test it from the terminal step by step, so you can verify that positive integer counter works as expected.

Step 1: Wrap Your Logic in a Named Function (For Easy Testing)

First, let's turn that anonymous lambda + count-if combo into a reusable named function and save it in a .lisp file. This makes it way easier to call from the terminal later.

Create a file named count-positives.lisp with this content:

;; count-positives.lisp
(defun count-positive-numbers (input-list)
  "Counts the number of positive integers in the provided list."
  (count-if #'(lambda (x) (> x 0)) input-list))

Step 2: Compile the .lisp File

We'll use SBCL (a popular open-source Common Lisp implementation) for this example—adjust commands slightly if you're using CLISP, CCL, or another implementation.

From the Terminal (One-Liner)

Run this command to compile the file and exit automatically:

sbcl --load count-positives.lisp --eval "(compile-file \"count-positives.lisp\")" --quit

This will generate a compiled bytecode file named count-positives.fasl (the exact extension might vary by implementation—CLISP uses .fas instead).

Alternatively: From the SBCL Interactive REPL

If you prefer working in the REPL first:

  1. Launch SBCL with sbcl in the terminal.
  2. Load your file:
    * (load "count-positives.lisp")
    
  3. Compile it:
    * (compile-file "count-positives.lisp")
    
  4. Exit the REPL with (quit).

Step 3: Run the Compiled Code and Test Your List

Now that you have a compiled file, you can call the function directly from the terminal with your test list (1 2 3 4 5).

Run this command:

sbcl --load count-positives.fasl --eval "(print (count-positive-numbers '(1 2 3 4 5)))" --quit

You should see 5 printed to the terminal—exactly your expected result!

Quick Alternative: Skip Separate Compilation (For Testing)

If you just want to test quickly without compiling first, you can run the source file directly:

sbcl --load count-positives.lisp --eval "(print (count-positive-numbers '(1 2 3 4 5)))" --quit

Compiling is better for performance with larger codebases, but this works great for quick tests.

For CLISP Users

If you're using CLISP instead of SBCL, here's how to adjust:

  1. Compile the file:
    clisp -c count-positives.lisp
    
  2. Run the test:
    clisp -i count-positives.fas -x "(print (count-positive-numbers '(1 2 3 4 5)))"
    

内容的提问来源于stack exchange,提问作者Pedro Guzman Iii

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 05:24:00