# lec2 - if x=0 then else x countdown(x-1 fun append(lst1 int...

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

(* CSE 341, Spring 2008 *) (* Lecture 2: functions, pairs, and lists *) (* recursion *) fun pow (x:int, y:int) = (* only correct for y >= 0 *) if y=0 then 1 else x * pow(x,y-1) fun cube (x:int) = pow(x,3) (* pairs *) fun swap (pr : int*bool) = (#2 pr, #1 pr) fun sum_two_pairs (pr1 : int*int, pr2 : int*int) = (#1 pr1) + (#2 pr1) + (#1 pr2) + (#2 pr2) (* returning a pair a real pain in Java *) fun div_mod (x : int, y : int) = (x div y, x mod y) fun sort_pair (pr : int*int) = if (#1 pr) > (#2 pr) then pr else ((#2 pr),(#1 pr)) (* lists *) fun sum_list (lst : int list) = if null lst then 0 else hd(lst) + sum_list(tl(lst)) fun countdown (x : int) =

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if x=0 then else x :: countdown(x-1) fun append (lst1 : int list, lst2 : int list) = if null lst1 then lst2 else hd(lst1) :: append(tl(lst1), lst2) (* lists of pairs *) fun sum_pair_list (lst : (int * int) list) = if null lst then 0 else #1 (hd(lst)) + #2 (hd(lst)) + sum_pair_list(tl(lst)) fun firsts (lst : (int * int) list) = if null lst then else (#1 (hd lst))::(firsts(tl lst)) fun seconds (lst : (int * int) list) = if null lst then else (#2 (hd lst))::(seconds(tl lst)) fun sum_pair_list2 (lst : (int * int) list) = (sum_list (firsts lst)) + (sum_list (seconds lst))...
View Full 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 / 2

lec2 - if x=0 then else x countdown(x-1 fun append(lst1 int...

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

View Full Document
Ask a homework question - tutors are online