Lec13 - basic definitions(define a 3(define b a 2 basic function(define cube1(lambda(x x x x many functions take a variable number of

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
; basic definitions (define a 3) (define b (+ a 2)) ( ; basic function (define cube1 (lambda (x) (* x (* x x)))) ; many functions take a variable number of arguments (define cube2 (lambda (x) (* x x x))) ; syntactic sugar for function definitions (define (cube3 x) (* x x x)) ; conditional (define (pow1 x y) (if (= y 0) 1 (* x (pow1 x (- y 1))))) ; currying (define pow2 (lambda (y) (lambda (x) (pow1 x y)))) (define cube4 (pow2 3)) ( ; list processing (define (sumlist lst) (if (null? lst) 0 (+ (car lst) (sumlist (cdr lst))))) (define (append lst1 lst2) (if (null? lst1) lst2 (cons (car lst1) (append (cdr lst1) lst2)))) ; dynamic typing: sum list that has numbers or ; other lists of (numbers or other lists of. ..) (define (sum1 lst) (if (null? lst) 0 (if (number? (car lst)) (+ (car lst) (sum1 (cdr lst))) (+ (sum1 (car lst)) (sum1 (cdr lst)))))) ; cond is better style than nested if-else (define (sum2 lst) (cond [(null? lst) 0] [(number?(car lst)) (+ (car lst)(sum2 (cdr lst)))] [#t (+ sum2 (car lst)) (sum2 (cdr lst))])) ; an infinite loop that does not type-check in ML
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/12/2009 for the course CSE 341 taught by Professor Staff during the Spring '08 term at University of Washington.

Page1 / 3

Lec13 - basic definitions(define a 3(define b a 2 basic function(define cube1(lambda(x x x x many functions take a variable number of

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online