2021-08-28 10:52:47 +00:00
|
|
|
title: Functions
|
2021-08-29 06:24:18 +00:00
|
|
|
type: concept
|
2021-08-28 10:52:47 +00:00
|
|
|
summary: |
|
|
|
|
Functions describe a process of turning *inputs* into *outputs*.
|
|
|
|
|
|
|
|
content: |
|
|
|
|
In a purely mathematical setting, functions typically have one input and one
|
|
|
|
output, but in functional programming, we can usually get around this either
|
|
|
|
by using [tuples][1] or by [currying][2].
|
|
|
|
|
2021-08-28 20:08:35 +00:00
|
|
|
[1]: page://fp-tuples
|
|
|
|
[2]: page://fp-currying
|
2021-08-28 10:52:47 +00:00
|
|
|
|
|
|
|
exercises:
|
|
|
|
|
2021-08-28 18:03:38 +00:00
|
|
|
- name: doubleIt
|
2021-08-28 20:08:35 +00:00
|
|
|
style: gradedProgram
|
2021-08-28 18:03:38 +00:00
|
|
|
description: |
|
2021-08-28 10:52:47 +00:00
|
|
|
Write a function called `doubleIt` that takes an integer and doubles it.
|
|
|
|
|
2021-08-28 18:03:38 +00:00
|
|
|
graders:
|
2021-08-28 10:52:47 +00:00
|
|
|
ocaml:
|
|
|
|
style: studentModule
|
|
|
|
props:
|
|
|
|
interface: |
|
|
|
|
val doubleIt : int -> int
|
|
|
|
driver: |
|
|
|
|
open List
|
|
|
|
let () = List.iter
|
|
|
|
(fun x -> assert ((doubleIt x) = (x * 2)))
|
|
|
|
(List.init 100 (fun x -> x + 1));
|
|
|
|
|
2021-08-28 20:08:35 +00:00
|
|
|
- name: whichIsFunction
|
|
|
|
style: multipleChoice
|
|
|
|
description: |
|
|
|
|
Which of the following can be described as a _function_?
|
|
|
|
|
|
|
|
graders:
|
|
|
|
ocaml:
|
|
|
|
style: multipleChoice
|
|
|
|
props:
|
|
|
|
foo: bar
|