Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

ML Programming Lab: Anonymous Functions and Higher-Order Functions, Lab Reports of Computer Science

Instructions for a lab session in cecs 424 course on ml programming. The lab focuses on learning anonymous functions and higher-order functions in ml through examples and exercises. Students are asked to write and test various function definitions and function calls using anonymous functions and higher-order functions.

Typology: Lab Reports

Pre 2010

Uploaded on 08/19/2009

koofers-user-a10-1
koofers-user-a10-1 🇺🇸

10 documents

1 / 2

Toggle sidebar

Related documents


Partial preview of the text

Download ML Programming Lab: Anonymous Functions and Higher-Order Functions and more Lab Reports Computer Science in PDF only on Docsity! CECS 424 Lab on 9/27/05 Do the following: 1. Start SML for ML programming. 2. Learn to program with anonymous functions and higher order functions in ML. Type each of the following lines and see what happens: a) Anonymous functions i) Note ML’s response to the following two function definitions and function calls fun f x = x + 2; (* defines a named function f *) f 1; fn x => x + 2; (* defines an anonymous function that does the same thing as f *) (fn x => x + 2) 1; ii) (* Example on p.137 quicksort (L,lt) -- Returns a sorted copy of list L, sorted in order of the less-than predicate lt. We use the quicksort algorithm, always choosing the first element of the list as the pivot. *) fun quicksort (nil, lt) = nil | quicksort (pivot::rest, lt) = let (* This function splits a list into two parts: those elements lt pivot and those elements not lt pivot. We return a this pair of lists. *) fun split(nil) = (nil,nil) | split(x::xs) = let val (below, above) = split(xs) in if lt(x,pivot) then (x::below, above) else (below, x::above) end; val (below, above) = split(rest) in quicksort (below, lt) @ [pivot] @ quicksort (above, lt) end; quicksort ([1, 4, 3, 2, 5], fn (a, b) => a < b); (* quicksort is called with an anonymous function *) quicksort ([1, 4, 3, 2, 5], fn (a, b) => a > b); (* quicksort is called with an anonymous function *) quicksort ([1, 4, 3, 2, 5], op <); (* quicksort is called with a predefined operator *) quicksort ([1, 4, 3, 2, 5], op >); (* quicksort is called with a predefined operator *)
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved