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 7736943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:52:23+00:00 2026-06-01T07:52:23+00:00

I’m looking to call functions dynamically based on the contents found in an association

  • 0

I’m looking to call functions dynamically based on the contents found in an association list.

Here is an example in semi-pseudo-code. listOfFunctions would be passed to callFunctions.

listOfFunctions = [('function one', 'value one')
                   , ('function two', 'value two')
                   , ('function three', 'value three')]

callFunctions x = loop through functions
                  if entry found 
                  then call function with value
                  else do nothing

The crux of the question is not looping through the list, rather, it’s how to call a function once I have it’s name?

Consider this use case for further clarification. You open the command prompt and are presented with the following menu.

1: Write new vHost file

2: Exit

You write the new vHost file and are not presented with a new menu

1: Enter new directive

2: Write file

3: Exit

You enter some new directives for the vHost and are now ready to write the file.

The program isn’t going to blindly write each and every directive it can, rather, it will only write the ones that you supplied. This is where the association list comes in. Writing a giant if/then/else or case statement is madness. It would be much more elegant to loop through the list, look for which directives were added and call the functions to write them accordingly.

Hence, loop, find a function name, call said function with supplied value.

Thanks to anyone who can help out with this.

Edit:

Here is the solution that I’ve come up with (constructive critiques are always welcome).

I exported the functions which write the directives in an association list as every answer provided said that just including the function is the way to go.

funcMap = [("writeServerName", writeServerName)
           ,("writeServeralias", writeServerAlias)
           ,("writeDocRoot", writeDocRoot)
           ,("writeLogLevel", writeErrorLog)
           ,("writeErrorPipe", writeErrorPipe)
           ,("writeVhostOpen", writeVhostOpen)]

In the file which actually writes the hosts, that file is imported.

I have an association list called hostInfo to simulate some dummy value that would be gathered from an end-user and a function called runFunction which uses the technique supplied by edalorzo to filter through both the lists. By matching on the keys of both lists I ensure that the right function is called with the right value.

import Vhost.Directive

hostInfo =     [("writeVhostOpen", "localhost:80")
                ,("writeServerName", "norics.com")] 

runFunctions = [f val | (mapKey, f) <- funcMap, (key, val) <- hostInfo, mapKey == key]
  • 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-06-01T07:52:24+00:00Added an answer on June 1, 2026 at 7:52 am

    Since I am farily new to Haskell I will risk that you consider my suggestion very naive, but anyways here it goes:

    let funcs = [("sum", (+3),1),("product", (*3),2),("square", (^2),4)]
    [f x | (name, f, x) <- funcs, name == "sum"]
    

    I think it satisfies the requirements of the question, but perhaps what you intend is more sofisticated than what I can see with my yet limitted knowledge of Haskell.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i got an object with contents of html markup in it, for example: string
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.