Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6648429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:39:15+00:00 2026-05-26T00:39:15+00:00

I implemented a simple state machine in Python: import time def a(): print a()

  • 0

I implemented a simple state machine in Python:

import time

def a():
    print "a()"
    return b

def b():
    print "b()"
    return c

def c():
    print "c()"
    return a


if __name__ == "__main__":
    state = a
    while True:
        state = state()
        time.sleep(1)

I wanted to port it to C, because it wasn’t fast enough. But C doesn’t let me make a function that returns a function of the same type. I tried making the function of this type: typedef *fn(fn)(), but it doesn’t work, so I had to use a structure instead. Now the code is very ugly!

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

typedef struct fn {
    struct fn (*f)(void);
} fn_t;

fn_t a(void);
fn_t b(void);
fn_t c(void);

fn_t a(void)
{
    fn_t f = {b};

    (void)printf("a()\n");

    return f;
}

fn_t b(void)
{
    fn_t f = {c};

    (void)printf("b()\n");

    return f;
}

fn_t c(void)
{
    fn_t f = {a};

    (void)printf("c()\n");

    return f;
}

int main(void)
{
    fn_t state = {a};

    for(;; (void)sleep(1)) state = state.f();

    return EXIT_SUCCESS;
}

So I figured it’s a problem with C’s broken type system. So I used a language with a real type system (Haskell), but the same problem happens. I can’t just do something like:

type Fn = IO Fn
a :: Fn
a = print "a()" >> return b
b :: Fn
b = print "b()" >> return c
c :: Fn
c = print "c()" >> return a

I get the error, Cycle in type synonym declarations.

So I have to make some wrapper the same way I did for the C code like this:

import Control.Monad
import System.Posix

data Fn = Fn (IO Fn)

a :: IO Fn
a = print "a()" >> return (Fn b)

b :: IO Fn
b = print "b()" >> return (Fn c)

c :: IO Fn
c = print "c()" >> return (Fn a)

run = foldM (\(Fn f) () -> sleep 1 >> f) (Fn a) (repeat ())

Why is it so hard to make a state machine in a statically typed language? I have to make unnecessary overhead in statically typed languages as well. Dynamically typed languages don’t have this problem. Is there an easier way to do it in a statically typed language?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:39:15+00:00Added an answer on May 26, 2026 at 12:39 am

    If you use newtype instead of data, you don’t incur any overhead. Also, you can wrap each state’s function at the point of definition, so the expressions that use them don’t have to:

    import Control.Monad
    
    newtype State = State { runState :: IO State }
    
    a :: State
    a = State $ print "a()" >> return b
    
    b :: State
    b = State $ print "b()" >> return c
    
    c :: State
    c = State $ print "c()" >> return a
    
    runMachine :: State -> IO ()
    runMachine s = runMachine =<< runState s
    
    main = runMachine a
    

    Edit: it struck me that runMachine has a more general form; a monadic version of iterate:

    iterateM :: Monad m => (a -> m a) -> a -> m [a]
    iterateM f a = do { b <- f a
                      ; as <- iterateM f b
                      ; return (a:as)
                      }
    
    main = iterateM runState a
    

    Edit: Hmm, iterateM causes a space-leak. Maybe iterateM_ would be better.

    iterateM_ :: Monad m => (a -> m a) -> a -> m ()
    iterateM_ f a = f a >>= iterateM_ f
    
    main = iterateM_ runState a
    

    Edit: If you want to thread some state through the state machine, you can use the same definition for State, but change the state functions to:

    a :: Int -> State
    a i = State $ do{ print $ "a(" ++ show i ++ ")"
                    ; return $ b (i+1)
                    }
    
    b :: Int -> State
    b i = State $ do{ print $ "b(" ++ show i ++ ")"
                    ; return $ c (i+1)
                    }
    
    c :: Int -> State
    c i = State $ do{ print $ "c(" ++ show i ++ ")"
                    ; return $ a (i+1)
                    }
    
    main = iterateM_ runState $ a 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We need to implement a simple state machine in C . Is a standard
We have issues within an application using a state machine. The application is implemented
Is it feasible to use the yield keyword to implement a simple state machine
I've used the State pattern to implement a simple finite state machine. Looking at
Finite state machine A deterministic finite state machine is a simple computation model, widely
I have implemented a simple file upload-download mechanism. When a user clicks a file
In our desktop application, we have implemented a simple search engine using an inverted
I have the simple class using auto-implemented properies: Public Class foo { public foo()
Is there a simple way to retrieve the length of an associative array (implemented
Simple question: I'm researching SMS gateways for the U.S. We've implemented in Europe before,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.