Learning functional programming and category theory: Monad, Monoid, Complexity

Here’s a video that really helped me to understand all the monad and monoid in functional programming. Also I got a new intuition for category theory as well.

It’s not a perfect video. For me, because I’m a beginner, the tempo of the explaination was sometimes too fast. There are places where it’s very slow as well. But it’s this kind of video that you can’t help but do the pause-rewind-google  digging. Very nice for beginners!

Here are some notes I took when watching the video and from searching for relevant explanation:
Monoid: identity + associativity
It’s about how to combine function, how to build up complexity, take complexity (side effect) into control

Type constructor = extra data

Monad is just composition of functions which return a different type
Bind is the tricky part in Monads (because it lacks symmetry)
Selectmany or flatmap is an example of bind

A bit of history:
Bottom up camp: turning machine, start from the hardware to the abstract
Top down camp: lambda calculus, start from the abstract to implementation

Python this week: for loops, scripting, etc.

I know I shouldn’t write for loops anymore since I’ll be doing functional programming. But it’s still faster for me to do stuff imperatively. Hopefully the transition to FP will come smoothly and soon! (Maybe a post on the learning process. I’m having lots of fun!)

For now, here’s the monstrous 3 level for loop I wrote to do the computation (comparison actually) in this post and this post. There’s actually another one hidden and not shown here because it happens way in the beginning with variable i indicating which music piece it is. But even without any background, I think what I’m trying to say is the way to arrange the order of for loops and how to make hierarchy out of this. It’s pretty straight forward but just need some thinking:


When there are lots of for loops, there is a hierarchy of some kind. The way to encode every hierarchy (maximum hierachicality maybe?) in python is to put an empty list before every for loop, and collect the output after the computation in the for loop (I didn’t collect them all because one level of hierachy didn’t matter to me, and I used both list and dict in this one). The order of the for loop (over which variable we should iterate first) is the tricky part. One easy way to start is to think which level of the hierarchy we would like to display first, and put that in the outermost loop. 

About scripting, I think it’s again something which I would frown upon myself in the future. But for the speed I need and my limited programming knowledge, I do use lots of scripts. It’s my way to speed things up with losing the control and reproducibility. It’s very easy to create scripts and keep the complexity of each script under control when the project is small. Naming is also important here: I’m really bad at it, so only when the project is small, I can handle it; otherwise I lose track that which script is associate with which functionality. Good documentation can help with this. 

Well, modularity and compositional complexity always go hand in hand. But the latter is probably more interesting. 


Art Museums and Stendhal syndrome

Stendhal syndrome, Stendhal’s syndrome, hyperkulturemia, or Florence syndrome is a psychosomatic disorder that causes rapid heartbeat, dizziness, fainting, confusion and even hallucinations when an individual is exposed to an experience of great personal significance, particularly viewing art.

This past year, I’ve experienced this syndrome many times, mostly from going to art museums (e.g. Philly Bernes’ foundation, Boston fine art, NYC MOMA and Metropolitan, Seattle modern art and fine art, Chicago fine art, Bruxelles, Paris Louvre and Pompidou, London Tate, etc. )  plus museums of instruments, museums of pop culture, museums of science and technology, planetariums, aquariums, etc.

Let’s start with arts and leave others for another time. Here are some of my favourites:

I like surrealism the most. It’s always interesting to interpret them which usually gives people a new awareness and perspective of a personal or social problem.  Impressionism paintings are very beautiful and I enjoy standing back and forth in front of the painting just try to see what’s actually on the painting and what we see. I also have a soft spot for painting about instruments or musicians. Exhibitions with new experimental materials also drew me in.

I remember the first time I went to an art exhibition is when I was about 15. I was disappointed by the random strokes and abstract shapes (it was a modern art exhibition :P). I clearly remembered that I thought (and probably as many other people would have thought): I can do that kind of scribble too. But the truth is, 10 years later, I haven’t even attempt any “serious” scribble and I can’t even find a daily life scribble which I want to present to someone else. And then it hit me, not only the technics, also the reason why “the scribble” exists, why it is here on this wall, is something interesting.


I interviewed a violinist who said the museums are her favourite places to go. It cultures sensitivity. The reason I tried the first few pricy museum was because of her words actually. But it’s so addictive!  Museums are one of my favourite places to go when I got to a new city now 🙂 Going to the ones in Netherlands soon!

I don’t draw nor paint very much myself, either have I learnt any art history. So I hope my thinking is not on the “wrong” side. But I’ll definitely try to draw some more (maybe even post them sometimes yeah). And some other realisations might come in the future!

Arriving in the Netherlands

Ok, after my trip to Long Island, Boston, Rochester, Chicago, Denver, Seattle and Beijing, now I’m finally in Utrecht!

It’s been two weeks since I’m here in this cute little city. It’s not my first time here: the last time was five years ago, in 2012! I’m living in the same building I was staying back then. Lots of memories.

But anyway, the most exciting thing is that a new direction, functional programming, is going to be added to my current music informatics research. After two weeks of meeting new people, taking a new course, reading new papers in this field, I’m very intrigued. There’re definitely lots of mathematics involved. And one has to learn a different way of thinking and programming. I can’t integrate the functional programming ideas in my work yet, but I’m definitely excited to see what would come out when that happens.

Besides research, I have joined a local Baroque ensemble, went to Zumba classes, jammed a little with office mates, drew and coloured some stuff, chatted with fellow international students, etc. One disappointment is that the practice room and the courses provided are not regulated well at Parnassos, the Utrecht University’s cultural centre (probably will write a complaint post about his haha). But I would be unfair to compare everywhere with Eastman I guess 😛


I think I’m getting to the point where I’m not too surprised when something didn’t go as I imagined how it would go. I will analyse it, compare it to the best I’ve seen, to the worst I’ve seen, figure out why it happened and how to prevent or try to make it happens again. And keep focused. I hope it’s a good thing.