racket function函数操作示例代码

代码语言:racket

所属分类:其他

代码描述:racket function函数操作示例代码

代码标签: 操作 示例

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

#lang racket
(lambda () "Hello World") ; => #<procedure>
;; Can also use a unicode `λ'
(λ () "Hello World")     ; => same function

;; Use parens to call all functions, including a lambda expression
((lambda () "Hello World")) ; => "Hello World"
((λ () "Hello World"))      ; => "Hello World"

;; Assign a function to a var
(define hello-world (lambda () "Hello World"))
(hello-world) ; => "Hello World"

;; You can shorten this using the function definition syntactic sugar:
(define (hello-world2) "Hello World")

;; The () in the above is the list of arguments for the function
(define hello
  (lambda (name)
    (string-append "Hello " name)))
(hello "Steve") ; => "Hello Steve"
;; ... or equivalently, using a sugared definition:
(define (hello2 name)
  (string-append "Hello " name))

;; You can have multi-variadic functions too, using `case-lambda'
(define hello3
  (case-lambda
    [() "Hello World"]
    [(name) (string-append "Hello " name)]))
(hello3 "Jake") ; => "Hello Jake"
(hello3) ; => "Hello World"
;; ... or specify optional arguments with a default value expression
(define (hello4 [name "World"])
  (string-append "Hello " name))

;; Functions ca.........完整代码请登录后点击上方下载按钮下载查看

网友评论0