美文网首页
common lisp函数式编程

common lisp函数式编程

作者: 李2牛 | 来源:发表于2018-05-16 15:18 被阅读0次

Lisp 是什么?

这是一门创造于1958年的古老语言,是函数式编程的鼻祖,拥有众多方言,极其容易DIY。
这里讨论的是ANSI 标准化的Common Lisp。

clisp 安装

mac 下根据使用的软件包管理器选择命令安装 clisp

brew install clisp 
port install clisp

第一行代码

lisp 第一行代码

lisp语法

  1. 每一句 Lisp 都需要使用圆括号(parentheses)进行标记
  2. 注释语句有以下几种


    lisp的注释
  3. 语法使用实例
  • 变量与函数
;;;;程序开始的时候如果需要注释说明需要四个分号
;;;大部分使用三个即可
    ;;带有缩进的注释需要两个分号
    ;;(print "hello");一行代码末尾的注释一个分号即可
(format t " hello world ~%");;格式化输出,lisp不是大小写敏感的语言
(print "What's your name?");;输出语句
(defvar *name* (read));;定义变量并从终端读取,变量必须使用两个星号(asterisk)之间
(defun hello-you(*name*)
    (format t "Hello  ~a! ~%" *name* );;显示变量,~%换行
    (format t "Hello  ~s! ~%" *name* )
    (format t "Hello  ~10a! ~%" *name* );;右边加10空格
    (format t "Hello  ~10@a! ~%" *name* );;左边加10个空格

)
( hello-you *name*);;调用语句
#||
多行注释
||#
变量与函数
  • 数学运算
;;;;设置输出的格式
(setq *print-case* :capitalize)


(defvar *name* 6990000);;定义变量并设置默认的数值
(format t "defalut name = ~a! ~%" *name*)
(setf *name* ( + 500988 (- 60000 2)) );;更改变量的数值
(format t "now name with commas = ~:d ~% "*name*);;带有逗号输出


(format t "PI to 7 characters is ~7f ~%" 3.14159265358);;截取七个字符
(format t "PI to 7 decimals  is ~,7f ~%" 3.14159265358);;七位小数 
(format t "10 Percent ~,,f ~%" 0.10)
(format t "Dollar 10 ~$ ~% " 10);;保留两位


#||

下面测试四则运算

||#

(format t "(+ 4 5) = (~d) ~%" (+ 4 5))
(format t "(- 4 5) = (~d) ~%" (- 4 5))
(format t "(* 4 5) = (~d) ~%" (* 4 5))
(format t "(/ 4 5) = (~d) ~%" (/ 4 5))
(format t "(/ 4 5) = (~f) ~%" (/ 4 5))
(format t "(/ 4 5.0) = (~d) ~%" (/ 4 5.0))
(format t "(rem 23 5) = (~d) ~%" (rem 4 5))
(format t "(mod 23 5) = (~d) ~%" (mod 4 5))

;;;常用数学函数

(format t "(expt 4 5) = (~d) ~%" (expt 4 2));;求幂exponent
(format t "(sqrt 81) = (~d) ~%" (sqrt 81));;开方
(format t "(exp 1) = (~d) ~%" (exp 1));;e^1
(format t "(log 1000 10) = (~d) ~%" (log 1000 10));;求对数log(10)1000
(format t "(max 4 5) = (~d) ~%" (expt 4 5));;求最大
(format t "(min 4 5) = (~d) ~%" (expt 4 5));;求最小
(format t "(floor 4.5) = (~d) ~%" (floor 4.5));;向下取整
(format t "(ceiling 4.5) = (~d) ~%" (ceiling 4.5));;向上取整
(format t "(oddp 23) = (~d) ~%" (oddp 23));;是否是奇数
(format t "(evenp 6) = (~d) ~%" (evenp 6));;是否偶数
(format t "(numberp 4.5) = (~d) ~%" (numberp 4.5));;是否为数字
(format t "(null nil) = (~d) ~%" (null nil));;是否为空
;;;此外lisp 还内置了sin,cos,tan,asin,acos等函数
数学运算与输出
(setq *print-case* :capitalize)

(defparameter *name* 'kent);定义变量的默认数值

(format t "(equal *name* 'kent) = ~d ~%" (equal *name* 'kent));;变量是否相等
(format t "(equal *name* 'knet) = ~d ~%" (equal *name* 'knet));;变量是否相等
(format t "(equal \"String\"  \"string\") = ~d ~%" (equal "String" "string"))
(format t "(equal (list 1 2 3) (list 1 2 3)) = ~d ~%" (equal (list 1 2 3 ) (list 1 2 3)));;表是否相等

(print "testEqualp")
(format t "( equalp 2 2.0) = ~d ~%" (equalp 2 2.0));;忽略格式
(format t "(equalp \"String\"  \"string\") = ~d ~%" (equalp "String" "string"));;忽略大小写
equalp and equal
  • 流程控制
(setq *print-case* :capitalize)

(print "your age ? ~%")
(defvar *age*  (read))
(print "your nationality?")
(defparameter *nationality* (read))

(if (and (>= *age* 18) (or (equalp *nationality* 'America) (equalp *nationality* 'USA) ))
    (format t "you can watch some adult video ~%");;if true
    (format t "go to study !!!~%");;else
)

(if (and (>= *age* 18) (or (equalp *nationality* 'America) (equalp *nationality* 'USA) ))
    (progn (format t "you can watch some adult video ~%")
        (format t "but donnot get addicted in it")
    );;if true
    (format t "go to study !!!~%");;else
)


(defun get-school(age) 
    (case age
        (5 (print "kindergarten ~%"))
        (6 (print "first grade ~%"))
        (otherwise (print "middle school"))
    )
)
(get-school *age*)

(when (= *age* 18)
    (print " go to University ~%")
    )

(unless (> *age* 30)
    (print " work hard please ~%")
    )
流程控制,addicted 应该和to搭配哈哈

相关文章

  • 浅谈函数式编程

    1.什么是函数式编程? 函数是一等公民,一切都是函数。 2.常用语言? Lisp各种方言:Common Lisp:...

  • common lisp函数式编程

    Lisp 是什么? 这是一门创造于1958年的古老语言,是函数式编程的鼻祖,拥有众多方言,极其容易DIY。这里讨论...

  • 函数式编程FP

    函数式编程的特点 1.函数式编程 是一种编程规范 也就是如何编写程序的方法论 2.函数式编程最早出现在LISP语...

  • common lisp - 函数

    函数不仅是 Lisp 程序的根基,它同时也是 Lisp 语言的基石。 除了少数称为特殊形式 (special fo...

  • 简析JavaScript中的函数式编程(一)

    与Lisp,Haskell不同,JavaScript并非函数式编程语言,但在JS之中可以像操控对象一样操控函数,换...

  • AI编程范式 第0章 前言

    人工智能编程范式:Common Lisp案例学习Peter Norvig 前言 范式(paradigm):名词,一...

  • Python入门知识——Python 函数式编程入门教程

    Functional Programming(函数式编程)的概念最早起源于LISP,由约翰·麦卡锡在1958年创立...

  • Python 函数式编程入门教程

    引言 Functional Programming(函数式编程)的概念最早起源于LISP,由约翰·麦卡锡在1958...

  • Python 函数式编程入门教程

    引言 Functional Programming(函数式编程)的概念最早起源于LISP,由约翰·麦卡锡在1958...

  • 前端开发函数式编程入门

    前端开发函数式编程入门 函数式编程是一门古老的技术,从上个世纪60年代Lisp语言诞生开始,各种方言层出不穷。各种...

网友评论

      本文标题:common lisp函数式编程

      本文链接:https://www.haomeiwen.com/subject/knqcdftx.html