-3

I was doing an assignment for my class, where I have to add up all of the items within a list. I was confused about how to do it, since I was told that you can not access individual items within a list like how you can in Python.

The way that I found how most people did it was using something like the following method

let rec list_sum lst =
    match lst with
    | [] -> 0
    | hd :: tl -> hd + list_sum tl

My question is, where does the hd and tl come from? The people who wrote the code never predefined these variables or anything, so are you allowed to just write them like that? How do they know what list that you are talking about?

halfer
  • 19,824
  • 17
  • 99
  • 186
Kyle Meng
  • 9
  • 1
  • 6
  • You need to take several hours to read a good book (or course) on Ocaml. You also need to read the [Ocaml manual](https://caml.inria.fr/pub/docs/manual-ocaml-4.07/). StackOverflow cannot teach you something which requires a full book to answer – Basile Starynkevitch Sep 10 '18 at 02:55
  • Our teacher did not give us a book to read, and the online web pages did not explain any of this – Kyle Meng Sep 10 '18 at 02:56
  • Then you should use Google (or your preferred search engine) to find more resources on Ocaml and on pattern matching. There are many of them. Did you read the [pattern matching](https://en.wikipedia.org/wiki/Pattern_matching) wikipage? – Basile Starynkevitch Sep 10 '18 at 02:56
  • And that is what I have done. But when I go onto a page that explains matching or lists, every one of them skips over this idea. – Kyle Meng Sep 10 '18 at 02:57
  • Using Stack was my final option after searching over 6 or 7 websites. I only use it as a last resort when all other options fail – Kyle Meng Sep 10 '18 at 02:58
  • So, continue your research on the Web. I remember having read many resources about your question (there are many introductory courses on Ocaml, in various languages: English, French, Russian and others ...). Searching only 7 web sites is not enough. – Basile Starynkevitch Sep 10 '18 at 02:58
  • If I can find an answer from an actual human being that can explain it to me, rather than using another half an hour searching through websites that do not get to the point and have me search through pages and pages of information just to find what I need, I would much rather ask someone else. – Kyle Meng Sep 10 '18 at 03:01
  • 1
    Because the time of people answering you is at least as valuable as your own – Basile Starynkevitch Sep 10 '18 at 03:03
  • Give at least in your question some links to the resources you have read – Basile Starynkevitch Sep 10 '18 at 03:05
  • Then why do people come onto this site? If you would not like to answer a question, then please just skip it, but if there are people who are willing to answer my question, then why should you criticize? This website is made to ask and answer questions. – Kyle Meng Sep 10 '18 at 03:05
  • 1
    They don't go on SO for basic questions like yours. You should go to your university library, and/or ask your teacher. – Basile Starynkevitch Sep 10 '18 at 03:10
  • First of all, my campus is a 20 minute drive from where I live, there is a story to this, and my instructor does not answer emails, which creates a problem when I want to try and finish an assignment at night. And second of all, yes they do. In the last 20 minutes while I was searching on Stack, I have found many many questions that ask much simpler questions than I did, such as what does the :: mean in pattern matching, what does in mean as well. This site does not restrict you on what you can ask. – Kyle Meng Sep 10 '18 at 03:15
  • 2
    [This site has a whole bunch of restrictions on what you can ask](https://stackoverflow.com/help/on-topic). And here's the first result from searching for "ocaml pattern matching list":: _[... the variables hd and tl are bound by the pattern that defines the second case of the match statement. Variables that are bound in this way can be used in the expression to the right of the arrow for the pattern in question.](https://v1.realworldocaml.org/v1/en/html/lists-and-patterns.html)_. RWO is a good resource to learn OCaml. You should read through it. – glennsl Sep 10 '18 at 09:42
  • 2
    See also https://stackoverflow.com/q/2502354/124319 – coredump Sep 10 '18 at 13:49
  • Kyle: don't take the criticism above to heart. It is worth trying to understand it though: readers want to know you have researched the topic exhaustively before asking, so that helpers can focus on the questions where it is clear an effort has been made beforehand. You've mentioned in the comments that you read "6 or 7 websites", but we hear that sort of thing so often, we are not sure it is true in every case. One of the ethics reflected here is "we help here that have helped themselves first"; it serves to weed out the vampires who do not intend to make any effort. – halfer Sep 15 '18 at 15:56
  • It is unfair that different tags here are policed differently; sadly, extraordinarily trivial and unresearched questions in SQL and CSS can be asked with relative impunity, since there are a lot of able helpers willing to encourage lazy authors. Perhaps it is not so in this tag, and it probably also is not in C++ either. How the community achieves a consistency here is a difficult question - it may of course not be possible at all. – halfer Sep 15 '18 at 16:00

2 Answers2

1

The pattern hd :: tl introduces the names hd and tl. That's one of the purposes of a pattern, in essence. To give names to the parts of a value that matches.

When you say match v with pattern ... the pattern is matched against the value v. In this code, the patterns are matched against the value lst.

Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108
1

My question is, where does the hd and tl come from? The people who wrote the code never predefined these variables or anything, so are you allowed to just write them like that?

The variables are declared by using them in the pattern. The code could just as well have been

let rec list_sum lst =
    match lst with
    | [] -> 0
    | fred :: ethel -> fred + list_sum ethel

How do they know what list that you are talking about?

The code declares a function called list_sum that takes a parameter called lst. When you use the function, you supply an argument for that parameter, and that indicates which list you are "talking about," like this:

let some_numbers = [ 2; 3; 4; 5 ]
let the_sum = list_sum some_numbers
phoog
  • 42,068
  • 6
  • 79
  • 117