mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-18 04:21:25 +02:00
Improve functions intro.
Add `λ', sugared/unsugared examples, optionals, keywords.
This commit is contained in:
@@ -204,44 +204,71 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d'
|
|||||||
;; 3. Functions
|
;; 3. Functions
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
;; Use lambda to create new functions.
|
;; Use `lambda' to create functions.
|
||||||
;; A function always returns its last statement.
|
;; A function always returns the value of its last expression
|
||||||
(lambda () "Hello World") ; => #<procedure>
|
(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"
|
((lambda () "Hello World")) ; => "Hello World"
|
||||||
|
((λ () "Hello World")) ; => "Hello World"
|
||||||
|
|
||||||
;; Assign a function to a var
|
;; Assign a function to a var
|
||||||
(define hello-world (lambda () "Hello World"))
|
(define hello-world (lambda () "Hello World"))
|
||||||
(hello-world) ; => "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")
|
(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
|
(define hello
|
||||||
(lambda (name)
|
(lambda (name)
|
||||||
(string-append "Hello " name)))
|
(string-append "Hello " name)))
|
||||||
(hello "Steve") ; => "Hello Steve"
|
(hello "Steve") ; => "Hello Steve"
|
||||||
|
;; ... or equivalently, using a sugared definition:
|
||||||
|
(define (hello2 name)
|
||||||
|
(string-append "Hello " name))
|
||||||
|
|
||||||
;; You can have multi-variadic functions, too
|
;; You can have multi-variadic functions too, using `case-lambda'
|
||||||
(define hello2
|
(define hello3
|
||||||
(case-lambda
|
(case-lambda
|
||||||
[() "Hello World"]
|
[() "Hello World"]
|
||||||
[(name) (string-append "Hello " name)]))
|
[(name) (string-append "Hello " name)]))
|
||||||
(hello2 "Jake") ; => "Hello Jake"
|
(hello3 "Jake") ; => "Hello Jake"
|
||||||
(hello2) ; => "Hello World"
|
(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
|
;; Functions can pack extra arguments up in a list
|
||||||
(define (count-args . args)
|
(define (count-args . args)
|
||||||
(format "You passed ~a args: ~a" (length args) args))
|
(format "You passed ~a args: ~a" (length args) args))
|
||||||
(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)"
|
(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
|
;; You can mix regular and packed arguments
|
||||||
(define (hello-count name . args)
|
(define (hello-count name . args)
|
||||||
(format "Hello ~a, you passed ~a extra args" name (length args)))
|
(format "Hello ~a, you passed ~a extra args" name (length args)))
|
||||||
(hello-count "Finn" 1 2 3)
|
(hello-count "Finn" 1 2 3)
|
||||||
; => "Hello Finn, you passed 3 extra args"
|
; => "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
|
;; 4. Equality
|
||||||
|
Reference in New Issue
Block a user