tags:

views:

72

answers:

1

I have to create a function about peano numbers defined as the following datatype:

datatype 'a peano = P of ('a -> 'a) * 'a -> 'a
val zero = P(fn (f, x) => x)

The function that I have to implement finds the succesive peano number of the peano parameter P(p). This is what I have written:

fun suc (P(p)) = case P(p) of P(fn(f,x)=>x) => P(fn(f,x)=>f(x));

The problem is that i get these errors:

stdIn:4.33-4.36 Error: syntax error: deleting FN LPAREN
stdIn:4.43 Error: syntax error found at RPAREN

I don't know what Im doing wrong. Please help!

+1  A: 

There are a number of problems in this code. The one the compiler is whining about is that you have a function definition

fn (f,x) => x

on the left-hand side of a case arm, where only patterns are permitted.

Some other problems:

  1. Redundant parentheses make the code hard to read (advice is available on removing them).
  2. Your case expression is redundant; in the function definition

     fun suc (P p) = ...
    

    it should be possible just to compute with p without any more case analysis.

  3. Since P carries a function, you will probably have an easier time if you write

     fun suc (P f) = ...
    

    and make sure that in the result, f is applied to a pair (as required by the datatype declarations).

Norman Ramsey