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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:27:34+00:00 2026-05-17T15:27:34+00:00

I am always interested in learning new languages, a fact that keeps me on

  • 0

I am always interested in learning new languages, a fact that keeps me on my toes and makes me (I believe) a better programmer. My attempts at conquering Haskell come and go – twice so far – and I decided it was time to try again. 3rd time’s the charm, right?

Nope. I re-read my old notes… and get disappointed 🙁

The problem that made me lose faith last time, was an easy one: permutations of integers.
i.e. from a list of integers, to a list of lists – a list of their permutations:

[int] -> [[int]]

This is in fact a generic problem, so replacing ‘int’ above with ‘a’, would still apply.

From my notes:

I code it first on my own, I succeed. Hurrah!

I send my solution to a good friend of mine – Haskell guru, it usually helps to learn from gurus – and he sends me this, which I am told, “expresses the true power of the language, the use of generic facilities to code your needs”. All for it, I recently drank the kool-aid, let’s go:

permute :: [a] -> [[a]]
permute = foldr (concatMap.ins) [[]]
   where ins x []     = [[x]]
         ins x (y:ys) = (x:y:ys):[ y:res | res <- ins x ys]

Hmm.
Let’s break this down:

bash$ cat b.hs
ins x []     = [[x]]
ins x (y:ys) = (x:y:ys):[ y:res | res <- ins x ys]

bash$ ghci
Prelude> :load b.hs
[1 of 1] Compiling Main             ( b.hs, interpreted )
Ok, modules loaded: Main.

*Main> ins 1 [2,3]
[[1,2,3],[2,1,3],[2,3,1]]

OK, so far, so good. Took me a minute to understand the second line of “ins”, but OK:
It places the 1st arg in all possible positions in the list. Cool.

Now, to understand the foldr and concatMap. in “Real world Haskell”, the DOT was explained…

(f . g) x

…as just another syntax for…

f (g x) 

And in the code the guru sent, DOT was used from a foldr, with the “ins” function as the fold “collapse”:

*Main> let g=concatMap . ins
*Main> g 1 [[2,3]]
[[1,2,3],[2,1,3],[2,3,1]]

OK, since I want to understand how the DOT is used by the guru, I try the equivalent expression according to the DOT definition, (f . g) x = f (g x) …

*Main> concatMap (ins 1 [[2,3]])

<interactive>:1:11:
     Couldn't match expected type `a -> [b]'
            against inferred type `[[[t]]]'
     In the first argument of `concatMap', namely `(ins 1 [[2, 3]])'
     In the expression: concatMap (ins 1 [[2, 3]])
     In the definition of `it': it = concatMap (ins 1 [[2, 3]])

What!?! Why?
OK, I check concatMap’s signature, and find that it needs a lambda and a list, but that’s
just a human thinking; how does GHC cope? According to the definition of DOT above…

(f.g)x = f(g x), 

…what I did was valid, replace-wise:

(concatMap . ins) x y = concatMap (ins x y)

Scratching head…

*Main> concatMap (ins 1) [[2,3]]
[[1,2,3],[2,1,3],[2,3,1]]

So… The DOT explanation was apparently
too simplistic… DOT must be somehow clever enough to understand
that we in fact wanted “ins” to get curri-ed away and “eat” the first
argument – thus becoming a function that only wants to operate on [t]
(and “intersperse” them with ‘1’ in all possible positions).

But where was this specified? How did GHC knew to do this, when we invoked:

*Main> (concatMap . ins) 1 [[2,3]]
[[1,2,3],[2,1,3],[2,3,1]]

Did the “ins” signature somehow conveyed this… “eat my first argument” policy?

*Main> :info ins
ins :: t -> [t] -> [[t]]        -- Defined at b.hs:1:0-2

I don’t see nothing special – “ins” is a function that takes a ‘t’,
a list of ‘t’, and proceeds to create a list with all “interspersals”. Nothing about “eat your first argument and curry it away”.

So there… I am baffled. I understand (after an hour of looking at the code!) what goes on, but… God almighty… Perhaps GHC makes attempts to see how many arguments it can “peel off”?

  let's try with no argument "curried" into "ins",
  oh gosh, boom, 
  let's try with one argument "curried" into "ins",
  yep, works,
  that must be it, proceed)

Again – yikes…

And since I am always comparing the languages I am learning with what I already know, how would “ins” look in Python?

a=[2,3]
print [a[:x]+[1]+a[x:] for x in xrange(len(a)+1)]

[[1, 2, 3], [2, 1, 3], [2, 3, 1]]

Be honest, now… which is simpler?

I mean, I know I am a newbie in Haskell, but I feel like an idiot… Looking at 4 lines of code for an hour, and ending up assuming that the compiler… tries various interpretations until it finds something that “clicks”?

To quote from Lethal weapon, “I am too old for this”…

  • 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-17T15:27:35+00:00Added an answer on May 17, 2026 at 3:27 pm
    (f . g) x = f (g x)
    

    This is true. You concluded from that that

    (f . g) x y = f (g x y)
    

    must also be true, but that is not the case. In fact, the following is true:

    (f . g) x y = f (g x) y
    

    which is not the same.

    Why is this true? Well (f . g) x y is the same as ((f . g) x) y and since we know that (f . g) x = f (g x) we can reduce that to (f (g x)) y, which is again the same as f (g x) y.

    So (concatMap . ins) 1 [[2,3]] is equivalent to concatMap (ins 1) [[2,3]]. There is no magic going on here.

    Another way to approach this is via the types:

    . has the type (b -> c) -> (a -> b) -> a -> c, concatMap has the type (x -> [y]) -> [x] -> [y], ins has the type t -> [t] -> [[t]]. So if we use concatMap as the b -> c argument and ins as the a -> b argument, then a becomes t, b becomes [t] -> [[t]] and c becomes [[t]] -> [[t]] (with x = [t] and y = [t]).

    So the type of concatMap . ins is t -> [[t]] -> [[t]], which means a function taking a whatever and a list of lists (of whatevers) and returning a list of lists (of the same type).

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

Sidebar

Related Questions

Always was interested why are Array.Sort() and Array.IndexOf() methods made static and similar ArrayList.Sort()
Something I have always been interested in out of curiosity, is there a tool
I have always been interested in algorithms, sort, crypto, binary trees, data compression, memory
I always believe they did, but seeing some answers here make me doubt... Can
I always seem to have a hard time starting a new Firefox extension. Can
I always felt that expecting exceptions to be thrown on a regular basis and
Always when I run java application it will display in Windows Task Manager is
I always tend to forget these built-in Symfony functions for making links.
I always use unix timestamps for everything, but am wondering if there is a
I always seem to use Get when working with data (strongly typed or otherwise)

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.