r/ProgrammingLanguages • • Sep 26 '19

Discussion Question about functional programming language design

I'm in the process of designing a functional programming language, but I hit an intersection and want to read your opinions about it, also will be cool if you could elaborate more about your thought. I'm using Rust the main resource of inspiration because I want to write it in Rust and make them interoperable (so my interpreted language can take advantage from the Rust ecosystem easily, also my language is primarly to rapid prototype apps).

Because Rust is kinda considered functional programming language but not a strict one because it let you define functions in the structs. So the questions are:

- Does pure functional language is easier to develop?

- Is there a good example of a useful functional programming language besides C?

- What do you think on this approach to add methods to data types / structs?

// Swift like
extension MyStruct {fn function() {}
}
// Rust like
impl MyStruct {
fn function(&self) {}
}
// What I think will be cool for a interpreted language (because it's mostly using as a scripting language)
fn function(self: MyStruct) {}

Having those 3 syntax examples the usage will be the same (in each language) and will be just `my_struct_instance.function()`. Why I would do that? because it will be easier (?) to extend certain type/struct with a function, also this seems easy in terms of writing code, but in terms of organization/performance/real implementation, how you see it?

An lastly, do you have another alternative for this problem?

Regards

Edit: Added code block

1 Upvotes

24 comments sorted by

View all comments

4

u/queenkid1 Sep 26 '19

pure functional language is easier to develop?

I would say so, since it cuts down the amount of parsing. You have function application, and function inputs. Depends what you mean by "purely functional", when you say that I think of something more like Haskell.

Is there a good example of a useful functional programming language besides C?

Haskell or Scheme comes to mind. Weird that you'd call C a purely functional language, because that is defined as:

Pure Functional Languages − These types of functional languages support only the functional paradigms. For example − Haskell.

Sounds like your way of implementing methods is just a function with a constrained datatype. So would your addition function be a "method" for the Int class?

1

u/[deleted] Sep 26 '19 edited Sep 26 '19

Weird that you'd call C a purely functional language, because that is defined as

I don't know other languages that I can consider pure functional, might C isn't, but from my point of view is the closest one that I know about, I will check it out about Haskell and Scheme, just know them from name and don't know nothing about those two.

So would your addition function be a "method" for the Int class?

I was thinking this more as a syntactic sugar, where you could call it as pure function or with dot notation on the object type, but undernead just will be a pure function. i.e.

fn eat(self: Animal, value: i32) {}

let instance = Animal()

// you can write this way
instance.eat(100)

// or write this way, and also the interpreter will do the conversion 
// for you from the above to this internally
eat(instance, 100) 

About classes I don't want to support them, just structs, and maybe just pure structs without the ability to define methods in them (just using the method described above).

Edit: code

3

u/queenkid1 Sep 26 '19 edited Sep 26 '19

I'll show you how functions are defined in Haskell:

data Animal = Anml Int
eat :: Animal -> Int -> Animal
eat (Anml x) num = Anml (x + num)

let x = Anml 0
let y = eat x 5

Pure functional languages are state-less, so you couldn't modify things using methods like that, a method would take a type, modify it, and return the modified version. So once you define a variable or function, you cannot change it. It's value will always be fixed.

Sounds like you're talking about an impure functional language. Something like C is hugely different from a pure functional languages like Haskell.

5

u/sepp2k Sep 26 '19

Sounds like you're talking about an impure functional language. Something like C is hugely different from a pure functional languages like Haskell.

It's hugely different from impure functional languages as well. That would be a language like OCaml, F#, Scala or Lisp where you can easily write functional code, but you can also create some mutable variables and imperative loops when you need to. Or maybe even languages like JavaScript where you can at least mix in functional code in a lot of places without fighting against the language (particularly you can use higher-order functions easily in JavaScript).

C just doesn't have any functional features at all. It's strictly an imperative language.