One of the things I like about the new Apple language Swift is that it combines procedural programming with algebraic data types. What other languages do this?
-
meta.programmers.stackexchange.com/questions/6483/…– gnatCommented Jun 30, 2014 at 16:06
-
2Technically, this is neither a list nor shopping question. It has a simple, objective, canonical answer: "Yes".– Jörg W MittagCommented Jun 30, 2014 at 17:30
-
@JörgWMittag meta.stackexchange.com/a/183183/165773– gnatCommented Jun 30, 2014 at 18:39
-
Edited to address Jorg's point. Yes or no wasn't the answer I was hoping for ;)– John DeHopeCommented Jun 30, 2014 at 19:41
-
I'm not asking anyone to recommend anything.– John DeHopeCommented Jul 1, 2014 at 17:36
3 Answers
ML family: OCaml, SML, F#
ML was (AFAIK) the first language to directly support algebraic data types; ML and its descendants have always at least had mutable state through reference variables.
Racket
Within the Racket family there is #lang plai
which directly provides algebraic types.
#lang racket
supports the same style of program using multiple subtypes of a struct type and using the match
macro. There's a good talk on the relationship between algebraic types and this design in this talk, part of the Brown online PL course.
Typed Racket provides explicit union types and matching through the match
macro.
Scala
Scala's case classes are algebraic types built on top of the JVM's class system.
Others
Wikipedia has a list of languages with algebraic data types; languages on this list with mutability include Nemerle and haXe.
I've conflated match style operations with algebraic datatypes here, partly because convenience is really all that separates some of these languages from assembly; you can define ad-hoc algebraic types in any language you like, it just won't be very nice.
-
Thanks, that wikipedia page was pretty much what I was looking for. Appreciate it! Commented Jul 1, 2014 at 17:42
You can go full imperative in ML-family languages (e.g. Ocaml) while enjoying ADTs and pattern matching.
You can have a sort of ADT in Algol descendants like C and Pascal by using variant records. But you will get very little syntactic sugar. You'll have to pack/unpack and check the variants manually, without the static type safety that e.g. Haskell would give you.
-
I would dare say that you can go full imperative in Haskell too, by using monads. Maybe some Haskell expert can comment on this.– GiorgioCommented Jun 30, 2014 at 18:25
-
2@Giorgio Some people consider Haskell the "world's finest imperative language."– DovalCommented Jun 30, 2014 at 18:39
-
Java/C# don't support ADTs out of the box but with some boilerplate and lambdas you can pull it off.
If I'm not mistaken that's also pretty much what Scala's case classes are, minus the boilerplate.
-
Yes, Scala case classes are used to model algebraic data types. +1– GiorgioCommented Jun 30, 2014 at 18:26