# lec10 - v fun count_list2 f = List.foldl (fn (hd,acc)...

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

(* Dan Grossman, CSE341 Spring 2008, Lecture 10 *) ( (* A bunch of ways to cound 0s in a list *) ( (* List.foldl : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b List.length : 'a list -> int List.filter : ('a -> bool) -> 'a list -> 'a list *) fun count_zeroes_1 lst = case lst of [] => 0 | first::rest => (if first=0 then 1 else 0) + count_zeroes_1 rest fun count_zeroes_2 lst = List.length (List.filter (fn x => x=0) lst) val count_zeroes_3 = List.length o (List.filter (fn x => x=0)) v (* curry : ('a * 'b -> 'c) -> 'a -> 'b -> 'c *) fun curry f x y = f (x, y) f val count_zeroes_3' = List.length o (List.filter ((curry (op=)) 0)) v fun count_zeroes_4 lst = List.foldl (fn (x,y) => (if x=0 then 1 else 0) + y) 0 lst val count_zeroes_5 = List.foldl (fn (hd,acc) => (if hd=0 then 1 else 0) + acc) 0 fun count_list1 f lst = case lst of [] => 0 | hd::tl => (if f hd then 1 else 0) + count_list1 f tl val count_zeroes_6 = count_list1 (fn x => x=0) v val count_zeroes_6' = count_list1 (curry (op=) 0)

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: v fun count_list2 f = List.foldl (fn (hd,acc) =&gt; (if f hd then 1 else 0) + acc) 0 val count_zeroes_7 = count_list2 (fn x =&gt; x=0) v val count_zeroes_7' = count_list2 (curry (op=) 0) v (************) ( (** Examples for understanding type inference **) ( fun f x = let val (y,z) = x in (abs y) + z end fun sum lst = case lst of =&gt; 0 | hd::tl =&gt; hd + (sum tl) fun length lst = case lst of =&gt; 0 | hd::tl =&gt; 1 + (length tl) fun compose (f,g) = fn x =&gt; f (g x) f (***********) ( (** Simple use of modules for namespace management **) ( signature MATHLIB = sig val fact : int -&gt; int val half_pi : real val doubler : int -&gt; int end e structure MyMathLib :&gt; MATHLIB = struct fun fact x = if x=0 then 1 else x * fact (x - 1) val half_pi = Math.pi / 2.0 v fun doubler y = y + y end e val pi = MyMathLib.half_pi + MyMathLib.half_pi v val twenty_eight = MyMathLib.doubler 14...
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

lec10 - v fun count_list2 f = List.foldl (fn (hd,acc)...

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

View Full Document
Ask a homework question - tutors are online