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

  • Home
  • SEARCH
  • 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 207037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:42:00+00:00 2026-05-11T17:42:00+00:00

I was doing an exercise on F# Wiki Book on List (scroll to the

  • 0

I was doing an exercise on F# Wiki Book on List (scroll to the bottom) to create a Pair method.

I was able to pair a integer list without problem but an F# exception was thrown for a string list. It is just too cryptic for me to decipher what the exception means for an F# beginner like me.

Here is my initial attempt to implementing Pair on fsi.exe

> let pair l =
-     let rec loop acc = function
-         | [] -> acc
-         | (hd1 :: hd2 :: tl) -> loop ((hd1, hd2) :: acc) tl
-     List.rev(loop [] l)
-
- printfn "%A" ([1..10] |> pair)
- printfn "%A" ([ "one"; "two"; "three"; "four"; "five" ] |> pair);;

      let rec loop acc = function
  -----------------------^

stdin(2,24): warning FS0025: Incomplete pattern matches on this expression. 
    For example, the value '[_]' will not be matched

val pair : 'a list -> ('a * 'a) list

[(1, 2); (3, 4); (5, 6); (7, 8); (9, 10)]
Microsoft.FSharp.Core.MatchFailureException: 
Exception of type 'Microsoft.FSharp.Core.MatchFailureException' was thrown.
   at FSI_0002.clo@2T.Invoke(List`1 acc, List`1 _arg1)
   at FSI_0002.pair[T](List`1 l)
   at <StartupCode$FSI_0002>.$FSI_0002._main()
stopped due to error

So Pair does work on integer version
and the function signature

val pair : 'a list -> ('a * 'a) list

indicates that Pair operates on a generic list.

Question: Then why would Pair not work on a string list?

[ANSWER] (my version)
Simply returning accumulated list for else case (_) did the trick.
And the warning is taken care of, as well.

let pair l =
    let rec loop acc = function
//        | [] -> acc
        | (hd1 :: hd2 :: tl) -> loop ((hd1, hd2) :: acc) tl
        | _ -> acc
    List.rev(loop [] l)

printfn "%A" ([1..10] |> pair)
printfn "%A" ([ "one"; "two"; "three"; "four"; "five" ] |> pair)

[EDIT2] Well, I will also post my version of Unpair for completeness.

let unpair l = [for (a,b) in l do yield! a :: b :: []]

Here is somewhat flawed benchmarking using solution version against that of mine for 1 million item lists

#light

open System;

let pn l = printfn "%A" l

let duration f = 
    let startTime = DateTime.Now;
    let returnValue = f()
    let endTime = DateTime.Now;
    printfn "Duration (ms): %f" (endTime - startTime).TotalMilliseconds
    returnValue

let ll =  [for a in 1..1000000 do yield (a)]
let tl = [for a in 1..1000000 do yield (a,a)]


let pair1 l =
    let rec loop acc = function
        | [] | [_] -> List.rev acc
        | h1 :: h2 :: tl -> loop ((h1, h2) :: acc) tl
    loop [] l

let unpair1 l =
    let rec loop acc = function
        | [] -> List.rev acc
        | (h1, h2) :: tl -> loop (h2 :: h1 :: acc) tl
    loop [] l

let pair2 l =
    let rec loop acc = function
        | (hd1 :: hd2 :: tl) -> loop ((hd1, hd2) :: acc) tl
        | _ | [_] -> acc
    List.rev(loop [] l)

    let unpair2 l = [for (a,b) in l do yield! a :: b :: []]

pn(duration (fun() -> ll |> pair1))
pn(duration (fun() -> tl |> unpair1))

pn(duration (fun() -> ll |> pair2))
pn(duration (fun() -> tl |> unpair2))

Benchmark Result:

Solution version
PAIR -> Duration (ms): 255.000000
UNPAIR -> Duration (ms): 840.000000

My version
PAIR -> Duration (ms): 220.000000
UNPAIR -> Duration (ms): 1624.000000
  • 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-11T17:42:00+00:00Added an answer on May 11, 2026 at 5:42 pm

    I don’t think your version of Pair would work on a list of an odd number of anything. You happen to test an even number of ints and a odd number of strings. I think your second argument to match implies a list of at least two members. So you break off 2 break off 2 and get to a list with 1 element and none of your conditions match.

    [_] is a 1 item list with anything in it. You must provide a predicate that matches it.

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

Sidebar

Related Questions

Today I was doing the thread ring exercise from the Programming Erlang book and
Hey all. I'm doing a linked list exercise that involves dynamic memory allocation, pointers,
After doing my exercise in school, I use Sqlite to store my data. But
I'm doing an exercise to flatten nested lists. The code works in console but
I'm doing a book exercise that says to write a program that generates psuedorandom
As an exercise I'm trying to create a function that returns a generic list
I am doing some exercise from a book and have some problems with the
I'm doing an exercise as following: # B. front_x # Given a list of
I am a beginner in Java and am doing an exercise from a book.
i am doing an exercise from Orielly book, and they used that Twitter package

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.