⍝ Make it look like this

z←{n}Fit w;⎕IO;xval;⎕ML;sel

⍝ n-order quadratic fit

⎕IO ⎕ML←0 3

:If 0=⎕NC'n'

n←1

:EndIf

sel←w[1;]≢¨⊂⍬

z←sel/w

xval←z[0;]-z[0;0]

n⌊is ¯1+1⊃⍴z

z[1;]←(xval∘.*⍳1+n)+.×z[1;]⌹xval∘.*⍳1+n

z←sel\z

z[;(~sel)/⍳¯1↑⍴w]←(~sel)/w

3 Fit (⍳10),[¯.5]2

3 Fit (⍳10),[¯.5]2 2 2 2 2 3 3 3 3 3

Fit (⍳10),[¯.5]2 2 2 2 2 3 3 3 3 3

avg←{(+/⍵)÷⍴⍵}

mavg←{(⍺+/⍵)÷⍺}

⎕nl 9

x←?10⍴100

avg x

2 mavg x

ymd hms←{(3↑⍵) (3↑3↓⍵)}⎕ts

- Note that in the exploration instructions are shown as comments and in italics.
- Although it's important to understand the distinction between scalar and mixed functions there's no explicit syntax to force either.
- Just as primitive functions take their input from one or two arguments and return a result, so should defined functions.
- Describing the purpose of a function with a comment in the first line is good practice.
- Insulating defined functions from the outside world (localising index origin and emulation level, for example) makes them universally and thoughtlessly useable.
- Make temporary variables that are used within functions local.
- It is not a good idea to use or set global variables within functions.
- There's a convention that mixed functions take "controlling" values through their left argument and "data" values through the right.
- There are two main styles for defining functions, the "traditional" style, shown here with "Fit", and "dynamic functions" - shown here with "avg" and "mavg".
- While executing, APL generally doesn't distinguish between the two styles.
- Dynamic functions don't exist in APLs other than Dyalog.
- There are subtle (and not-so-subtle) differences in how the two styles of function behave.
- Complete applications have been written using either style exclusively.
- Dynamic function definition is often useful for creating (unnamed) in-line functions

- Function Syntax (Dyalog Language Reference)
- Traditional function definition (Dyalog Language Reference) - but ignore the section on the APL Line Editor
- Dynamic functions (Dyalog Language Reference)
- Edit, Debug and Trace windows (Dyalog User Guide)
- Syntax Colouring (Dyalog User Guide)

- Define a "traditional" function which returns the square of each number in its argument.
- Modify the function so that the left argument (if present) specifies the power to which the right argument is to be raised (allow the left argument to be either a scalar or an array with the same shape as the right argument. If the left argument is omitted assume a value of 2.
- Define an equivalent dynamic function.
- Define functions to left-justify, right-justify and centre-justify character matrices (both traditional and dynamic forms).
- Write a function which takes a date (as a three-element numeric vector, for example 31 3 2008) and returns the last day in the preceding month (as a character vector in the form Friday 29 February 2008)