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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:58:12+00:00 2026-06-18T03:58:12+00:00

real basic question here: I’m new to Ocaml and I’m having issues trying to

  • 0

real basic question here: I’m new to Ocaml and I’m having issues trying to manipulate lists. I’ve read http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html, and I’m unfortunately still confused….I’m new to functional programming.

If I have, say, the following function:

  let stoverfl list1 list2 list3 =
      match list1 with
      |[]->None
      |h::list1 -> (*what I want to do goes in here*)

I want to take a look at the first elements of list2 and list3, compare them, and if they’re equal, add the first element of list3 to list2, else don’t modify the lists. I don’t really care about error checking now (i.e. checking to see if the list has at least one element, etc.).

My attempt:

 h::list1 -> let cmp1 = hd list2 (*this should return the first elemnt of list2??*)
             let cmp2 = hd list3
             if(cmp1=cmp2) then
                let updlist2 = concat list2 hd list3
                let updlist3 = hd list3
                (*pass updlist2 and updlist3 instead of list2 and list3 to next function*)
             else
                (*do nothing; pass list2 and list3 as normal*)

I feel like I’m doing it all wrong…any advice would be appreciated!
Thanks.

  • 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-18T03:58:13+00:00Added an answer on June 18, 2026 at 3:58 am

    You say you want to add the first element of list3 to list2. When a functional programmer says something like this what it means is that they want to construct a new list as described. You can’t actually modify list2–lists are immutable and names are permanently bound to a single value. In fact, it seems like you want to construct two new lists. One of them has the first element of list2 at the front of list3. The other one is the remainder of list2 (all but the first element).

    Assuming this is what you mean, and assuming you’re not worried whether the lists are empty, then here’s one way to get these values:

    let updlist2 = List.tl list2 in
    let updlist3 = (List.hd list2) :: list3
    

    It’s actually pretty close to what you wrote.

    However, I wonder what you’re going to do with these values next. If you want to pass them along to another function, it would be more normal to have the lets outside the if. As an example, here’s some code that calls a function g with either a list, or the tail of the list, depending on the first element of the list:

    let arg_for_g =
        match the_list with
        | [] -> []  (* No tail of the list *)
        | head :: tail -> if head = 3 then tail else the_list
    in
        g arg_for_g
    

    If you put the lets inside the if, you’ll need to code two different calls to your next function. That could be fine, it depends on exactly what you’re going to do with your values.

    (As a side note, you should worry about whether the lists are empty! If you use a match statement you can detect that case pretty easily.)

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

Sidebar

Related Questions

Real basic CI question here, which I cant find anything on in the documentation.
this is a real basic question, but I am having a frustrating time learning
I'm a Java newbie, and this is a real basic, basic question, so please
Question pretty much says it all. You can read entity association here , but
My first question here and i'll try to be specific. I am quite new
Noobie Alert. Ugh. I'm having some real trouble getting some basic file I/O stuff
I'm pretty new to C# and am having a mare trying to get what
I have a real basic form (code below) with a bunch of back-panel PhP.
Real noob question no doubt, but I can't get my head around this behaviour.
Having real problems creating artifacts in teamcity 6.5 (using TFS & MSBuild as the

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.