美文网首页
scheme lisp 函数

scheme lisp 函数

作者: 悟剑声 | 来源:发表于2018-10-19 11:29 被阅读23次

由于Sheme是函数式编程语言,你需要通过编写小型函数来构造程序。因此,明白如何构造并组合这些函数对掌握Scheme尤为关键。在前端定义函数非常不便,因此我们通常需要在文本编辑器中编辑好代码,并在解释器中加载它们。

  • 定义简单函数并加载它们
    你可以使用define来将一个符号与一个值绑定。你可以通过这个操作符定义例如数、字符、表、函数等任何类型的全局参数。
    代码片段1(hello.scm)
; Hello world as a variable
(define vhello "Hello world")     ;1

; Hello world as a function
(define fhello (lambda ()         ;2
         "Hello world"))
(load "e:\\work\\scheme\\hello.scm")
;Loading "hello.scm" -- done
;Value: fhello
vhello
;Value 15: "Hello world"

fhello
;Value 16: #[compound-procedure 16 fhello]

(fhello)
;Value 17: "Hello world"

代码片段2 (farg.scm)

; hello with name
(define hello
  (lambda (name)
    (string-append "Hello " name "!")))

; sum of three numbers
(define sum3
  (lambda (a b c)
    (+ a b c)))
  • 一种函数定义的短形式
    用lambda定义函数是一种规范的方法,但你也可以使用类似于代码片段3中展示的短形式。
; hello with name
(define (hello name)
  (string-append "Hello " name "!"))

; sum of three numbers
(define (sum3 a b c)
  (+ a b c))

相关文章

网友评论

      本文标题:scheme lisp 函数

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