Standard ML help datatype problem

  • Thread starter Jimena29
  • Start date
  • Tags
    Standard
In summary, we have discussed how to create a function for Peano numbers defined as a datatype, how to convert Peano numbers to integers, and how to find the successor and double of a Peano number. We have also addressed a specific problem with the syntax of the code.
  • #1
Jimena29
4
0
Standard ML help! datatype problem

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

datatype 'a peano = P of ('a -> 'a) * 'a -> 'a


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 I am doing wrong please help!
 
Technology news on Phys.org
  • #2


So... it has been a very long time since I have done any S/ML and I don't have an interpreter handy, but at least maybe I can help you decode the error message.

When it says 4.33-4.36 it's referring to line 4, characters 33 through 36. What it says is the tokens "FN LPAREN" are not recognized in the place where you put them. This of course is "fn" followed by a "left parenthesis", which is indeed at the 33rd character position in your line of code.

Then when it gets to character 43 it hits a right parenthesis. Because it previously decided to ignore the lparen, the rparen of course also becomes a syntax error.

Are you absolutely sure it is legal to put an fn in that place, or in general on the right-hand side of a "case of"? The ml type system is very smart but it has limits. You seem to be wanting it to pattern match against a function that takes (f,x) and returns x. I really don't see how the pattern matcher could possibly know whether a function fits that pattern without evaluating the function (and really not even then). Maybe try to check your documentation or textbook and see exactly what kinds of things are legal after that "of". (And come to think of it, why are you pattern matching at all?-- that is, what is the point of a "case" statement with only one "of"?)
 
  • #3


Sorry, I didn't notice you had replied until now. But thank you so much!
If anyone has a problem like this I solved it, here's my code :
datatype 'a peano = P of ('a -> 'a) * 'a -> 'a

val zero = P(fn (f, x) => x)(* create : int -> 'a peano
* create(n) = the Peano number representing n
* Precondition: n >= 0)

fun create 0=zero
|create num =
let
fun helper (1,f,x) = f(x)
| helper (num,f,x) = helper (num-1,f,f(x))
in
P(fn(f,x)=>helper(num,f,x))
end;

(* peanoToInt : int peano -> int
* Postcondition: result >= 0
*)
fun peanoToInt (P(p)) = let fun f(x)=x+1 in p(f,0) end;

(* suc : peano -> 'a peano
* suc(p) = successor of the Peano number p
*)
fun suc (P(p)) = P(fn(f,x)=>p(f,f(x)));

* double : 'a peano -> 'a peano
* double p = (Peano number p) * 2
*)
fun double (P(p)) = P(fn(f,x)=> p(f,p(f,x)));
 

Related to Standard ML help datatype problem

What is Standard ML?

Standard ML is a functional programming language that is designed for writing and manipulating computer programs. It is a statically typed language that supports type inference and has a strong type system.

What is a datatype in Standard ML?

A datatype in Standard ML is a way of categorizing and organizing data. It specifies the type and structure of data that can be stored in a variable or passed as a function argument.

How do I declare a datatype in Standard ML?

To declare a datatype in Standard ML, you use the "datatype" keyword followed by the name of the datatype and a list of constructors that define the structure of the datatype.

What is a "help datatype problem" in Standard ML?

A "help datatype problem" in Standard ML refers to a situation where a programmer encounters an issue or difficulty with a datatype in their code and needs assistance in resolving it. This could include errors in datatype declarations or issues with using datatypes in functions.

Where can I find help with datatype problems in Standard ML?

You can find help with datatype problems in Standard ML through various resources such as online forums, documentation, and tutorials. You can also reach out to experienced Standard ML programmers for assistance.

Similar threads

  • Programming and Computer Science
Replies
8
Views
4K
  • Programming and Computer Science
Replies
15
Views
1K
Replies
1
Views
1K
  • Programming and Computer Science
Replies
13
Views
7K
  • Programming and Computer Science
Replies
1
Views
2K
  • Calculus and Beyond Homework Help
Replies
11
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Set Theory, Logic, Probability, Statistics
Replies
7
Views
2K
  • Differential Equations
Replies
1
Views
803
Back
Top