Improve functions intro.

Add `λ', sugared/unsugared examples, optionals, keywords.
This commit is contained in:
Eli Barzilay 2013-07-16 03:20:21 -04:00
parent d8e722d619
commit 8a065aa256

View File

@ -204,44 +204,71 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d'
;; 3. Functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Use lambda to create new functions.
;; A function always returns its last statement.
;; Use `lambda' to create functions.
;; A function always returns the value of its last expression
(lambda () "Hello World") ; => #<procedure>
;; Can also use a unicode `λ'
(λ () "Hellow World") ; => same function
;; (You need extra parens to call it)
;; 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 to:
;; You can shorten this using the function definition syntatcic sugae:
(define (hello-world2) "Hello World")
;; The () is the list of arguments for the function.
;; 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
(define hello2
;; You can have multi-variadic functions too, using `case-lambda'
(define hello3
(case-lambda
[() "Hello World"]
[(name) (string-append "Hello " name)]))
(hello2 "Jake") ; => "Hello Jake"
(hello2) ; => "Hello World"
(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 can pack extra arguments up in a list
(define (count-args . args)
(format "You passed ~a args: ~a" (length args) args))
(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)"
;; ... or with the unsugared `lambda' form:
(define count-args2
(lambda args
(format "You passed ~a args: ~a" (length args) args)))
;; You can mix regular and packed arguments
(define (hello-count name . args)
(format "Hello ~a, you passed ~a extra args" name (length args)))
(hello-count "Finn" 1 2 3)
; => "Hello Finn, you passed 3 extra args"
;; ... unsugared:
(define hello-count2
(lambda (name . args)
(format "Hello ~a, you passed ~a extra args" name (length args))))
;; And with keywords
(define (hello-k #:name [name "World"] #:greeting [g "Hello"] . args)
(format "~a ~a, ~a extra args" g name (length args)))
(hello-k) ; => "Hello World, 0 extra args"
(hello-k 1 2 3) ; => "Hello World, 3 extra args"
(hello-k #:greeting "Hi") ; => "Hi World, 0 extra args"
(hello-k #:name "Finn" #:greeting "Hey") ; => "Hey Finn, 0 extra args"
(hello-k 1 2 3 #:greeting "Hi" #:name "Finn" 4 5 6)
; => "Hi Finn, 6 extra args"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 4. Equality