This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
datatype 'a mylist = Cons of 'a * ('a mylist) | Empty | |
fun map f xs = | |
case xs of | |
Empty => Empty | |
| Cons(x,xs) => Cons(f x, map f xs) | |
fun filter f xs = | |
case xs of | |
Empty => Empty | |
| Cons(x,xs) => if f x then Cons(x.filter f xs) else filter f xs | |
fun lenght xs = | |
case xs of | |
Empty => 0 | |
| Cons(_,xs) => 1 + length xs | |
(* a valid way to double all numbers in the list | |
using partial application *) | |
val doubleAll = map (fn x => x * 2) | |
(* a way to count N's in a list using partial application *) | |
val countNs (xs, n:int) = length (filter (fn x => x=n) xs) |
No comments:
Post a Comment