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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:10:41+00:00 2026-05-16T01:10:41+00:00

I’m getting used to Haskell’s higher-order functions. Usually I can replace explicit patterns of

  • 0

I’m getting used to Haskell’s higher-order functions. Usually I can replace explicit patterns of recursion with functions like map, fold, and scan. However, I often run into the following recursion pattern which I don’t understand how to express using higher-order functions:

   f (x:[]) = k x
   f (x:xs) = g x (f xs)

For instance, suppose I am representing analytic tableaux. Then I create a data type such as:

   data Tableau = N Expr | S Expr (Tableau) | B Expr (Tableau) (Tableau)

If I want to convert a list of Exprs into a tableau structure, I want a function part of which might resemble:

   f (x:[]) = N x
   f (x:xs) = S x (f xs)

Now, I see three options: (1) create a function which decides, given a tableau and a list, whether the next branch in the tableau should be an S or N (or B, but we’ll ignore that case); (2) use a higher-order function to encapsulate the recursion pattern of f; (3) use a function like f.

What would the best option be?

  • 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-16T01:10:42+00:00Added an answer on May 16, 2026 at 1:10 am

    I’d most probably use the following:

    f xs = foldr g (k (last xs)) (init xs)
    

    It basically means that the end of the list is replaced by k x when folding. Thanks to lazy evaluation present everywhere, it works even for infinite lists.

    There are two other solutions – adding empty case and using Maybe.

    A) adding empty case:

    It would be best if f [] was well-defined. Then, you could write the definition as

    f [] = c
    f (x:xs) = g x (f xs)
    

    which is f = foldr g c. For example, if you change

    data Tableau = N Expr | S Expr Tableau | B Expr Tableau Tableau
    

    to

    data Tableau = N | S Expr Tableau | B Expr Tableau Tableau
    

    then you can represent single-element tableaux as S expr N, and the function is defined as one-liner

    f = foldr S N
    

    It’s the best solution as long the empty case makes sense.

    B) use Maybe:

    On the other hand, if f [] cannot be sensibly defined, it’s worse.
    Partial functions are often considered ugly. To make it total, you can use Maybe. Define

     f [] = Nothing
     f [x] = Just (k x)
     f (x:xs) = Just (g x w)
                where Just w = f xs
    

    It is a total function – that’s better.

    But now you can rewrite the function into:

     f [] = Nothing
     f (x:xs) = case f xs of
                  Nothing -> Just (k x)
                  Just w -> Just (g x w)
    

    which is a right fold:

     addElement :: Expr -> Maybe Tableaux -> Maybe Tableaux
     addElement x Nothing = Just (N x)
     addElement x (Just w) = Just (S x w)
    
     f = foldr addElement Nothing
    

    In general, folding is idiomatic and should be used when you fit the recursion pattern. Otherwise use explicit recursion or try to reuse existing combinators. If there’s a new pattern, make a combinator, but only if you’ll use the pattern a lot – otherwise it’s overkill. In this case, the pattern is fold for nonempty lists defined by: data List a = End a | Cons a (List a).

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I am trying to render a haml file in a javascript response like so:

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.