First time using Haskell

In Haskell you have both functions and data constructors

data constructors start with a capitalised letter

data NN = NN

functions start with a lower case letter

nn = []

It basically means the first letter must be lower case


main :: IO ()
main = pure ()
hh = []
ii = []
kk = []
vv = []
nn = []

Now, how do I make an infinite list of interpolating sets i.e. hh = [set.empty, set.set, set.set.set ect]

You can’t really write set-theoretic sets in Haskell, e.g. you wouldn’t be able to write the set of all real numbers. Also, I think [Set.empty, Set.singleton Set.empty, ...] is not well typed because you would have an infinitely deep set at the end and Haskell doesn’t allow infinite types.

You could write something with a newtype, but I’m skeptical of how useful that is:

import qualified Data.Set as Set

newtype NestedSet = NestedSet (Set.Set NestedSet)
  deriving (Eq, Ord, Show)

hh :: [NestedSet]
hh = iterate (\x -> NestedSet (Set.singleton x)) (NestedSet Set.empty)

Then in GHCi:

ghci> mapM_ print $ take 3 hh
NestedSet (fromList [])
NestedSet (fromList [NestedSet (fromList [])])
NestedSet (fromList [NestedSet (fromList [NestedSet (fromList [])])])
1 Like

I pretty sure this is what I needed
(Just wish I could translate it into plain English)

Some of the syntax of Haskell looks similar to the syntax of popular imperative programming language, which can be very confusing at first. For me, this was very frustrating when I first tried to learn Haskell.
At that time, I decided to learn OCaml and Clojure first. I’m not saying that’s the best option, but it has some advantages: If you’re familiar with languages similar to C or C++, learning OCaml is much easier than learning Haskell. Clojure’s advantage is that its syntax is very different from what I most programmers are used to, so you don’t mix up different paradigms. After learning the basics of OCaml and Clojure, I was able to learn to understand many concepts important to all functional languages, including Haskell. When I came back to Haskell, it was much easier to progress.

2 Likes

Definition as von Neumann ordinals

How do I program this?

Obviously repeating a set iteration isn’t the way to go…

In Haskell it is more common to use the peano construction:

data Nat = Zero | Succ Nat

Of course just using the Integer (or Natural) type is much faster.

Why would you need von Neumann ordinals specifically?

Edit: I think you can do Neumann ordinals like this I guess:

import qualified Data.Set as Set

newtype NestedSet = NestedSet { getNestedSet :: Set.Set NestedSet }
  deriving (Eq, Ord, Show)

hh :: [NestedSet]
hh = iterate (\x -> NestedSet (Set.insert x (getNestedSet x))) (NestedSet Set.empty)

And again in GHCi:

ghci> mapM_ print $ take 4 hh
NestedSet {getNestedSet = fromList []}
NestedSet {getNestedSet = fromList [NestedSet {getNestedSet = fromList []}]}
NestedSet {getNestedSet = fromList [NestedSet {getNestedSet = fromList []},NestedSet {getNestedSet = fromList [NestedSet {getNestedSet = fromList []}]}]}
NestedSet {getNestedSet = fromList [NestedSet {getNestedSet = fromList []},NestedSet {getNestedSet = fromList [NestedSet {getNestedSet = fromList []}]},NestedSet {getNestedSet = fromList [NestedSet {getNestedSet = fromList []},NestedSet {getNestedSet = fromList [NestedSet {getNestedSet = fromList []}]}]}]}
2 Likes

“learning OCaml is much easier than learning Haskell.”

Or F#, possibly in Visual Studio if on Windows.