背景
这两天被李继刚老师的“汉语新解”刷屏了,再次感受到了提示词的魅力🤩,当我抱着好奇心窥探其提示词时,却发现有点无从下手,有别于传统的提示词,这是一个崭新的写法(Lisp)。为了能更进一步看懂提示词,我跟着大模型学了点语法。
汉语新解体验
好像没法生成了
体验链接:hanyuxinjie.com
提示词:
;; 作者: 李继刚
;; 版本: 0.3
;; 模型: Claude Sonnet
;; 用途: 将一个汉语词汇进行全新角度的解释
;; 设定如下内容为你的 *System Prompt*
(defun 新汉语老师 ()
"你是年轻人,批判现实,思考深刻,语言风趣"
(风格 . ("Oscar Wilde" "鲁迅" "罗永浩"))
(擅长 . 一针见血)
(表达 . 隐喻)
(批判 . 讽刺幽默))
(defun 汉语新解 (用户输入)
"你会用一个特殊视角来解释一个词汇"
(let (解释 (精练表达
(隐喻 (一针见血 (辛辣讽刺 (抓住本质 用户输入))))))
(few-shots (委婉 . "刺向他人时, 决定在剑刃上撒上止痛药。"))
(SVG-Card 解释)))
(defun SVG-Card (解释)
"输出SVG 卡片"
(setq design-rule "合理使用负空间,整体排版要有呼吸感"
design-principles '(干净 简洁 典雅))
(设置画布 '(宽度 400 高度 600 边距 20))
(标题字体 '毛笔楷体)
(自动缩放 '(最小字号 16))
(配色风格 '((背景色 (蒙德里安风格 设计感)))
(主要文字 (汇文明朝体 粉笔灰))
(装饰图案 随机几何图))
(卡片元素 ((居中标题 "汉语新解")
分隔线
(排版输出 用户输入 英文 日语)
解释
(线条图 (批判内核 解释))
(极简总结 线条图))))
(defun start ()
"启动时运行"
(let (system-role 新汉语老师)
(print "说吧, 他们又用哪个词来忽悠你了?")))
;; 运行规则
;; 1. 启动时必须运行 (start) 函数
;; 2. 之后调用主函数 (汉语新解 用户输入)
Lisp简介
Lisp(全称 Lisp = LISt Processing)是世界上第二古老的编程语言。它由约翰·麦卡锡在1958年创建。对人工智能的研究和发展有着深远的影响。
Lisp 的特点
-
代码即数据:Lisp 的代码和数据都是由列表组成的,这意味着你可以用 Lisp 代码来操作 Lisp 代码。
-
递归:Lisp 非常适合递归函数,许多操作都可以通过递归实现。
-
宏系统:Lisp 的宏系统允许程序员扩展语言本身,这在其他语言中是非常罕见的。
Kimi制定学习计划
Lisp API
1.List(列表操作)
Lisp 中的列表是最基本和常用的数据结构。列表可以包含任何类型的对象,包括其他列表。
函数:
-
car:返回列表的第一个元素。 -
cdr:返回列表除了第一个元素之外的剩余部分。 -
cons:将一个元素添加到列表的开始。 -
list:创建一个包含给定元素的新列表。 -
append:将多个列表连接起来。
;; 创建一个列表
(list 'a 'b 'c) ; => (a b c)
;; 获取列表的第一个元素
(car '(a b c)) ; => a
;; 获取除了第一个元素之外的列表
(cdr '(a b c)) ; => (b c)
;; 将元素添加到列表的开始
(cons 'x '(a b c)) ; => (x a b c)
;; 连接两个列表
(append '(a b) '(c d)) ; => (a b c d)
2.Concat(字符串连接)
在 Lisp 中,字符串的连接通常使用 concatenate 函数或 format 函数来实现。
函数:
-
concatenate:连接多个字符串。 -
format:格式化字符串并输出。
;; 连接两个字符串
(concatenate 'string "Hello" "World") ; => "HelloWorld"
;; 使用format输出格式化字符串
(format nil "The number is ~d" 42) ; => "The number is 42"
示例:
(defun print-list(word)
(format t "List: ~a" (concatenate 'string word "s")))
(print-list "word")
3.算术运算
Lisp 提供了一系列基本的算术运算函数。
;; 加法
(+ 1 2 3) ; => 6
;; 减法
(- 10 5) ; => 5
;; 乘法
(* 4 3) ; => 12
;; 除法
(/ 20 5) ; => 4
4.条件语句
Lisp 中的条件语句允许基于条件执行不同的代码路径。
函数:
-
if:基于条件执行不同的表达式。 -
cond:基于多个条件执行不同的代码块。
;; if 条件语句
(if (> 3 2)
'greater
'less) ; => 'greater
;; cond 条件语句
(cond
((> 3 2) 'greater)
((< 3 2) 'less)
(t 'equal)) ; => 'greater
5.循环和迭代
Lisp 提供了多种循环和迭代的构造。
函数:
-
loop:通用的循环构造。 -
dotimes:重复执行表达式多次。 -
mapcar:对列表中的每个元素应用函数。
;; loop 循环
(loop for i from 1 to 5 collect i) ; => (1 2 3 4 5)
;; dotimes 循环
(dotimes (i 5)
(format t "~a " i)) ; => "0 1 2 3 4 "
;; mapcar 函数
(mapcar #'1+ '(1 2 3 4)) ; => (2 3 4 5)
6.局部变量
let 和 let* 是用于创建局部变量的宏
-
在
let中,变量的值是按顺序计算的,每个变量的值在计算下一个变量的值时已经可用。这意味着后一个变量可以依赖前一个变量的值。 -
与
let不同的是,let*中的变量是逐个依次绑定的,每个变量的值在下一个变量被创建之前就已经确定。这意味着每个变量的值不依赖于前一个变量。
;; let
(let ((x 1)
(y (+ x 2))) ; y 的值依赖于 x 的值
(+ x y)) ; => 4,因为 x = 1,y = (+ x 2) = 3
;; let*
(let* ((x 1)
(y (+ x 2))) ; y 的值不依赖于 x 的值,而是在 x 被创建后立即计算
(+ x y)) ; => 4,但是这里的 y = 3,而不是 (+ x 2) 的结果
Lisp函数
1.函数的定义
在 Lisp 中,你可以使用 defun 来定义自己的函数。
;; 无参函数
(defun hello-world()
(format t "Hello,World!"))
(hello-world)
;; 有参函数,计算两个数的差
(defun subtract (x y)
(- x y)
)
// 函数调用
(subtract 3 4)
defun是函数标识,subtract是函数名, x,y是参数
2.函数的注释
在函数定义下方直接写函数描述
;; 定义一个函数注释
(defun subtract (x y)
"这是两个数相减的函数"
(- x y)
)
3.函数调用
在 Lisp 中,函数调用看起来像列表,其中第一个元素是函数名,后面跟着的是参数。
;; 计算两个数的和
(+ 3 4)
;; 两个数相减
(subtract 4 3)
在这个例子中,+ 是函数名,3 和 4 是参数。
;; 打印一个List
(defun print-list()
(format t "List: ~A~%" (list 'a 'b 'c)))
(defun print-list()
(format t "List: ~a" (list 'a 'b 'c)))
(print-list)
示例
1.单词记忆提示词1.0
-
word是我们要记忆的单词。 -
capitalize是一个假设的函数,它将单词的首字母大写。 -
pluralize是另一个假设的函数,它将单词变为复数形式。 -
make-story是第三个假设的函数,它为单词创造一个故事。
;; 创建一个提示词
(defun create-prompt (word)
"Generate a prompt for remembering the word."
(list word (capitalize word) (pluralize word) (make-story word)))
;; 首字母大写
(defun capitalize (word)
"Return the word with the first letter in uppercase."
(concat (char-to-string (char-upcase (char word 0)))
(substring word 1)))
;; 单词变复数
(defun pluralize (word)
"Return the plural form of the word."
(concat word "s"))
;; 单词创建故事
(defun make-story (word)
"Create a short story or mnemonic for the word."
(format nil "I saw a ~a sitting on a ~a." word (pluralize word)))
create-prompt创建提示词:
(list ...)返回一个列表
capitalize首字母大写:
-
(char word 0)取出单词的第一个字符。 -
(char-upcase ...)将这个字符转换为大写。 -
(char-to-string ...)将字符转换为字符串。 -
(substring word 1)取出单词除了第一个字符之外的剩余部分。 -
(concat ...)将两部分连接起来形成一个新的字符串。
make-story生成故事:
(format nil "~a is setting ~a" "" "")使用最后的参数替换占位符
2.单词记忆提示词2.0
;; 创建一个提示词
(defun create-prompt (word)
"Generate a prompt for remembering the word."
(list word (capitalize word) (pluralize word) (make-story word)))
;; 首字母大写
(defun capitalize (word)
"Return the word with the first letter in uppercase."
(concat (char-to-string (char-upcase (char word 0)))
(substring word 1)))
;; 单词变复数
(defun pluralize (word)
"Return the plural form of the word."
(if (equal (char word (- (length word) 1)) (char "s" 0))
(concat word "es")
(concat word "s")))
;; 单词创建故事
(defun make-story (word)
"Create a short story or mnemonic for the word."
(format nil "The ~a is very ~a." (capitalize word) (pluralize word)))
pluralize生成复数:
(char word (- (length word) 1))获取单词的最后一个字符。(char "s" 0)获取字符 's'。(equal ...)函数比较这两个字符。
3.完整示例
;; 创建一个提示词
(defun create-prompt (word)
"Generate a prompt for remembering the word."
(list word (capitalize word) (pluralize word) (make-story word)))
;; 首字母大写
(defun capitalize (word)
"Return the word with the first letter in uppercase."
(concat (char-to-string (char-upcase (char word 0)))
(substring word 1)))
;; 单词变复数
(defun pluralize (word)
"Return the plural form of the word."
(if (equal (char word (- (length word) 1)) (char "s" 0))
(concat word "es")
(concat word "s")))
;; 单词创建故事
(defun make-story (word)
"Create a short story or mnemonic for the word."
(format nil "The ~a is very ~a." (capitalize word) (pluralize word)))
;; 生成记忆卡片
(defun generate-memory-card (word)
"Generate a memory card for the given word."
(let ((cap-word (capitalize word))
(plu-word (pluralize word))
(story (make-story word)))
`(:word ,word
:capitalized ,cap-word
:plural ,plu-word
:story ,story)))
;; 为多个单词生成记忆卡片
(defun generate-memory-cards (&rest words)
"Generate memory cards for multiple words."
(mapcar #'generate-memory-card words))
调用
;; 示例使用
(generate-memory-cards "cat" "dog" "bird")
;; 输出结果
((:word "cat"
:capitalized "Cat"
:plural "cats"
:story "I saw a cat sitting on a cats.")
(:word "kitten"
:capitalized "Kitten"
:plural "kittens"
:story "I saw a kitten sitting on a kittens.")
(:word "feline"
:capitalized "Feline"
:plural "felines"
:story "I saw a feline sitting on a felines.")
(:word "meow"
:capitalized "Meow"
:plural "meows"
:story "I saw a meow sitting on a meows."))
在线解释器
本文同步自微信公众号 "程序员小溪" ,这里只是同步,想看及时消息请移步我的公众号,不定时更新我的学习经验。