Clojure技术求助:无法解析symbol parse-to-dict及依赖导入错误
Hey there! Let's walk through fixing your Clojure code step by step—you've got a couple of small syntax, spelling, and logic issues causing those errors. Let's break them down:
1. Fix the Namespace Require Syntax
Your first compilation error comes from incorrect syntax in the ns declaration. When using :require, you don't need to wrap each dependency in single quotes, and there's no reason to require your own namespace (noobfile)—that creates an unnecessary circular dependency.
Wrong:
(ns noobfile (:require '[clojure.string :as str] '[noobfile]))
Correct:
(ns noobfile (:require [clojure.string :as str]))
2. Fix the Function Name Typo
The "unable to resolve symbol: parse-to-dict" error is a simple typo: you defined the function as pasre-to-dict (swapped s and r), but tried to call parse-to-dict. Correct the function name:
Wrong:
(defn pasre-to-dict [x] (str ":" x))
Correct:
(defn parse-to-dict [x] (str ":" x))
3. Fix String Splitting Logic
Your original my_str doesn't contain any newline characters (\n), so str/split my_str #"\n" will just return a single-element list. Based on your data structure, it looks like each entry starts with a number followed by |—you can either rewrite my_str with actual newlines for clarity, or use a regex that splits on whitespace preceding a number+|.
Example with Newlines:
(def my_str "1|John Smith|123 Here Street|456-4567 2|Sue Jones|43 Rose Court Street|345-7867 3|Fan Yuhong|165 Happy Lane|345-4533")
4. Fix apply Usage
apply passes all elements of a sequence as separate arguments to a function. Since your parse-to-dict handles one entry at a time, you should use map instead to apply the function to every element in the split list:
Wrong:
(apply parse-to-dict my_dict)
Correct:
(map parse-to-dict my_dict)
Full Corrected Code
Putting it all together, here's the working code:
(ns noobfile (:require [clojure.string :as str])) (def my_str "1|John Smith|123 Here Street|456-4567 2|Sue Jones|43 Rose Court Street|345-7867 3|Fan Yuhong|165 Happy Lane|345-4533") (def my_dict (str/split my_str #"\n")) (defn parse-to-dict [x] (str ":" x)) (map parse-to-dict my_dict)
When you run this, you'll get a sequence of formatted strings:
(":1|John Smith|123 Here Street|456-4567" ":2|Sue Jones|43 Rose Court Street|345-7867" ":3|Fan Yuhong|165 Happy Lane|345-4533")
内容的提问来源于stack exchange,提问作者optimal substructure

