99problems | 99 problems in different languages
kandi X-RAY | 99problems Summary
kandi X-RAY | 99problems Summary
99 problems in different languages
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of 99problems
99problems Key Features
99problems Examples and Code Snippets
Community Discussions
Trending Discussions on 99problems
QUESTION
I'm reading https://ocaml.org/learn/tutorials/99problems.html and it has 2 examples:
...ANSWER
Answered 2020-Apr-21 at 00:27The pattern _ :: t
doesn't mean what you say. It matches any non-empty list and calls the tail of the list t
.
The pattern h :: t
matches any non-empty list, calls the head of the list h
(one element, the first one), and the tail of the list t
(zero or more elements after the first one).
The operator ::
is the list constructor (often called "cons"), which is why these patterns match lists.
Here are examples of ::
as list constructor:
QUESTION
I'm solving problem 4 in the 99 problems of Ocaml and I'm still learning OCaml
Question 4 reads
OCaml standard library has List.length but we ask that you reimplement it. Bonus for a tail recursive solution.
So as per my understanding a recursive solution merits bonus points because it more efficient than a potentially easier more verbose solution
I came up with this for a tail recursive solution
...ANSWER
Answered 2020-Apr-01 at 18:20A tail-recursive function is a recursive function where all recursive calls happen in a tail position. What that means is that the recursive call must be the last thing that happens in any given path through the function. All of the recursive functions in your question are tail recursive.
Tail recursion does not mean recursing on the tail of the list. In fact a tail recursive function doesn't have to involve lists at all.
So a non-tail recursive function would be any function where you do anything after the recursive call (or there are even multiple recursive calls that aren't mutually exclusive). Usually that means applying some other function to the result of the recursive function after the recursive function returns.
A non-tail recursive version of length
would be:
QUESTION
I'm trying to implement a simple function: totient
:
ANSWER
Answered 2019-Mar-30 at 23:43The type Integral a => a -> a
says:
- Caller gets to choose a type
a
. - Caller must prove that
a
is an instance ofIntegral
. - Caller must provide a value of type
a
. - Implementer produces another value of type
a
.
However, in this case, the implementer has produced an Int
. Any time the caller chooses a
to be an instance of Integral
that is not Int
, this will not match the caller's type.
QUESTION
So I was trying question 48 from this link in LearnOcamel, and I got a syntax error with "in" keyword in the second let statement in the editor if you look at my provided codes. enter image description here
If I copy and past the codes in the first let statement the top level, and then copy and past the code of the second let statement in the top level, It works fine. enter image description here
This is really really strange. If I try to copy and past the entire codes in the top level then this won't work. enter image description here
...ANSWER
Answered 2019-Jan-15 at 20:20This phrase
QUESTION
Let's say we have a function that takes as input a set of Boolean variables: bol1,bol2...boln. How can we iterate through all possible Boolean assignments using minimum codes as possible? I need to implement a function that takes input a set of two Boolean variables together with a Boolean expression involving the variables and create a truth table. If you look at my code, It is long. SO I want to reduce it. Furthermore, the way I did it seem to be redudndant as the compiler gives warning saying the match case |Var v2 is unused for all match case |Var v2 in the code.
This is exercises 46/47 in this link : "Define a function, table2 which returns the truth table of a given logical expression in two variables (specified as arguments). The return value must be a list of triples containing (value_of_a, balue_of_b, value_of_expr)."
...ANSWER
Answered 2019-Jan-13 at 19:03Hint: try looking at the problem recursively.
QUESTION
I'm trying to implement a function, say f1, and I'm using previously defined function,say f2, to implement f1. f2 has multiple input like "f2 input1 input2 input3...". f1 return W.L.O.G input2 of f2. How can I correctly make the compiler know that I want f2 to accept the output of f1 as its input2 when using |> as in this statement: "f1 arg1 arg2... |> f2 input1 _ input3..."? If you look at my codes, the split function takes a list then an integer, but I want to give this function an list input using |>. The compiler gives error on "split e" in my function slice. It is expecting a list and not the number "e" when we do "l |> (split e)". You see that the function slice that I define doesn't follow the order of its arguments as required by the exercises. I did it so that I can use |> again to give slice a list input in another function f3 that uses the slice function. I'm wondering if this is necessary?
This is question 18 from this website for Ocaml exercises: "Given two indices, i and k, the slice is the list containing the elements between the i'th and k'th element of the original list (both limits included). Start counting the elements with 0 (this is the way the List module numbers elements)."
...ANSWER
Answered 2019-Jan-12 at 17:45You could use various tricky higher-level functions to reorder the parameters of your second function, but in my opinion the thing to do is just to bite the bullet and use let
. The use of |>
is not mandatory when composing functions!
Instead of this:
QUESTION
I'm implementing a method in Ocaml to perform run-length encoding data compression of a list of a random type 'a. Consecutive elements that are the same in the list is compressed into the type defined in the beginning of the provided code. My provided code dosen't work. One of the reason that I think it doesn't work is if you look at the new line between the big chuck of the first match cases, the matched cases after the new line should belong to the upper matched cases. However, the compiler in learn Ocaml cannot recognize that. It automatically grouped the remaining matched statements(which were supposed to be for the first match) to the nested match statement.
This is one of the excise in Ocaml tutorial website(Question 13): "Implement the so-called run-length encoding data compression method directly. I.e. don't explicitly create the sublists containing the duplicates, as in problem "Pack consecutive duplicates of list elements into sublists", but only count them. As in problem "Modified run-length encoding", simplify the result list by replacing the singleton lists (1 X) by X."
...ANSWER
Answered 2019-Jan-12 at 03:55I encounter this same problem with nested match
pretty often. You can solve it by parenthesizing the inner match.
QUESTION
The following code snippet comes from the official OCaml website:
...ANSWER
Answered 2018-Sep-22 at 18:05Yes, the base case is this:
QUESTION
I have the following Python tuple:
...ANSWER
Answered 2018-Apr-06 at 13:13So like:
QUESTION
I want to use an adjacency list to represent a graph structure, I don't need my edges to be weighted.
I want to practice simple exercises like finding a cycle, BFS, DFS , adding remove edges... nothing fancy. (I could do it with Hashtables as well, but I need more List
practice)
ANSWER
Answered 2017-Mar-19 at 17:44This is a perfectly good representation of a graph. You need to make sure the labels are unique. Also you have no distinction between the labels and other possible contents of the nodes.
With this structure, going from a label to the node with the label requires a search of the outer list. If your graphs get large, this is probably going to start taking too long. So you will need to construct an ancillary map from label to node. I have done this myself many times.
Another solution is to have node indices that are independent of the node contents. This also reduces the difficulty in dealing with duplicate labels. I'm working on graph problems right now and the structure is basically like this:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install 99problems
On a UNIX-like operating system, using your system’s package manager is easiest. However, the packaged Ruby version may not be the newest one. There is also an installer for Windows. Managers help you to switch between multiple Ruby versions on your system. Installers can be used to install a specific or multiple Ruby versions. Please refer ruby-lang.org for more information.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page