Skip to main content
gamedev.net
🔒 Locked

Functional language syntax

Started by Roboguy Oct 22, 2005 at 1:36 AM 3 replies 1.1k views
Original Post
Roboguy
Roboguy
I'm developing a (impure) functional language at the moment, and I've been trying to decide on a syntax. This is what I've come up with so far (not terribly original, but that isn't necessarily a bad thing):

factorial :: Integer a => a -> a  
let factorial
  (x == 0) { 1 }
  (x == 1) { 1 }
  (x > 1) { x * (factorial (x - 1)) };;

fibonacci :: Integer a => a -> a  
let fibonacci
  (x == 0) { 1 }
  (x == 1) { 1 }
  (x > 1) { (fibonacci (x - 1)) + (fibonacci (x - 2)) };;

map :: Any a, Any b => (a -> b) -> [a] ->   
let map
  _ [] { [] }
  f (x : xs) { (f x) : (map f xs) };;

reduce :: Any a => (a -> a -> a) -> [a] -> a  
let reduce
  f (x : []) { x }
  f (x : xs) { (f x (reduce f xs)) };;


interface Vector {
  length :: Void -> Float  
  normalize :: Void -> Vector  
};;

class Vector3D implements Vector {
  x, y, z :: Float, Float, Float  

  let length () {
    x = sqrt (self.x + self.y + self.z)
  };;

  let normalize () {
    instance Vector3D {
      let x = self.x / (self.length)  
      let y = self.y / (self.length)  
      let z = self.z / (self.length)  
    }
  }
};;
So, any comments and/or suggestions?
Kuladus
Kuladus
Looks like the deformed baby of haskell/ml and java!


Looks like some of the syntax is redundant. I.e. why not just do this?

fibonacci :: Integer a => a -> a    (x == 0) { 1 }  (x == 1) { 1 }  (x > 1) { (fibonacci (x - 1)) + (fibonacci (x - 2)) };


Also the class member function is slightly misleading:

let length () {    x = sqrt (self.x + self.y + self.z)}


Does this mean set self.x to the length or evaluate to x. This should also be in the functional style ... (for consistency)

length :: Float a => nil -> a   {sqrt (self.x**2 + self.y**2 + self.z**2)}




Other than that, I love functional programming!
Roboguy
Roboguy
Quote:
Original post by Kuladus
Looks like some of the syntax is redundant. I.e. why not just do this?

fibonacci :: Integer a => a -> a    (x == 0) { 1 }  (x == 1) { 1 }  (x > 1) { (fibonacci (x - 1)) + (fibonacci (x - 2)) };


Hmm, you're right; that does look nicer.

Quote:

Also the class member function is slightly misleading:

let length () {    x = sqrt (self.x + self.y + self.z)}


The x = was a typo.

Quote:

Does this mean set self.x to the length or evaluate to x. This should also be in the functional style ... (for consistency)

length :: Float a => nil -> a   {sqrt (self.x**2 + self.y**2 + self.z**2)}


Gah, forgot to square them also. I must be tired. That generic type variable representing a Float didn't occur to me, for some reason.
Kuladus
Kuladus
Quote:
That generic type variable representing a Float didn't occur to me, for some reason.


Actually it appears in _interface_ so it's not really required here.

cignox1
cignox1
Quote:

Looks like some of the syntax is redundant. I.e. why not just do this?

fibonacci :: Integer a => a -> a
(x == 0) { 1 }
(x == 1) { 1 }
(x > 1) { (fibonacci (x - 1)) + (fibonacci (x - 2)) };


if you plan to use the keyword 'let' for variables, I would use it for functions too:

let fibonacci :: Integer a => a -> a
(x == 0) { 1 }
(x == 1) { 1 }
(x > 1) { (fibonacci (x - 1)) + (fibonacci (x - 2)) };

They both are first class citizens, after all.
Anyway, good luck, I don't use functional languages, but once I had to study SML and OCAML, and I found them pretty interesting.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.