not sure what is wrong in this implementation [closed] - ocaml

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
type 'a tree = Leaf of 'a
|Fork of 'a tree * 'a tree
can someone show the right way to do this?

The compiler is telling you the following. You declared Fork like this:
| Fork of 'a tree * 'a tree
(Fixing a typo in your code btw.)
This indeed has two parameters, each of type 'a tree.
If you look at any of your Fork constructors you'll see that you're giving them three values. Here's one from the middle of your expression:
Fork(4, Leaf, Leaf)
You either need to change your definition of Fork or you need to supply only two values when using the Fork constructor.
If you want values in your internal nodes you should modify your definition of Fork to include a value (of type 'a presumably). If the values are all supposed to be at the leaves, you need to change your expression.
Update
Here is a working definition of a tree using your current definitions:
# let (t: int tree) = Fork (Leaf 1, Leaf 2);;
val t : int tree = Fork (Leaf 1, Leaf 2)
I should also point out that you're using Leaf with zero parameters, but you have declared it to take one parameter (as in my example here).

Related

How can I tell if a tuple is empty in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to know how I can identify if my tuple is empty?
Note that I use std::tuble in conjunction with std::map as below:
typedef std::tuple<SDESType,INT32,std::string> SDesItem;
typedef std::map<SDESType,SDesItem> SDesItemMap;
Now I want to check if the second value of SDesItemMap, that is a tuple, is empty or not?
if(SDesItems[SDESType] != NULL) // this is error
std::tuple acts like three values tied together. It has no predefined "empty" or "zero" state, other than empty states of each of its members. Empty value is always contextual, and in general means a value lying outside the domain of the regular type's values.
For example, a natural number can only be 0 (or 1, depending on your definition) upward. Thus, using an int gives you a convenient way to represent a value that's outside of the domain, and should be treated irregularly.
To represent that cleanly in your example, use optional<tuple<...>> as a map value (if you want to specifically state "there's an empty value at a given key"). This type states that the possible values of the type you get by instantiating the optional template are now all of the values of the original type, and a special, nothing (/empty/none) value.
You can also state that parts of the tuple can be empty (nothing), like
tuple <
std::string, // empty string ("") can be treated as "empty state"
optional<int>, // if you don't want to treat 0 (or -1) as "empty"
>
optional<> is not a part of std:: yet, so you have to write your own implementation or use the Boost one. Another way would be using value_ptr (Boost again), but here it wouldn't probably give you any benefits.

Haskell intersect two lists and remove double elements [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm new to Haskell and need an function to intersect two lists, but remove double elements.
If I load the Data.List
import Data.List
I can use this function:
intersect "abbcd" "abbe"
"abb"
The outcome is "abb", but what I need is:
intersectFunction "abbcd" "abbe"
"ab"
So the double char from the lists should display only once.
Any ideas?
perhaps the "unique" function named nub can help:
import Data.List
intersectFunction a b = nub $ intersect a b
You can use the intersect function you have, but then you need to remove the duplicate elements. It seems likely that there's a library function to do this, but how can we find it if we don't know the name?
The best way to answer questions of the form "Is there a Haskell function to do X" is usually:
Figure out what the type signature of the function you want would be.
In this case, we want a function that takes a list and produces a list. So the type signature is [a] -> [a]
Search hoogle or hayoo. If you don't find it in one, try the other. Usually it doesn't matter much whether you get the order of the input parameters exactly right, but you may need to experiment.
In this case, there are quite a few functions with that type signature. But if you go to the second page of results, you'll find the nub function, which does what you need.

Checking if element is a 0 or any other value in Scala [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a box made up of a list of lists in Scala and I need to check if a particular cell contains an element that is not 0 or a 0 (given the row and column index to access that cell). If the element found is not a 0, an empty set should be returned else another value which I managed to code should be returned. How can I check if a particular cell contains 0 or not?
Code so far:
if(//cell does not contain 0)
Set()
else
{
//this part of code is ready
}
as mentioned in a comment, just use box(i)(j).
Moreover, I would suggest to create at least a type alias for your box
type Box = List[List[Int]]
or a case class if you want to add methods like getValue(row: Int, col: Int) : Int
case class Box(val l: List[List[Int]])

Haskell about matching a Char with a list [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am looking for a way that when input a Char, then the function could match the list, like "QAZXSWEDCVFRTGBNHYUJMKIOPL", and then return the index of the matching one
For example, when input 'S', then it could find the 'S' is the 5th element in the list then return integer 4 (starting with 0).
You're looking for the function elemIndex from Data.List:
elemIndex :: Eq a => a -> [a] -> Maybe Int
-- Example
main = do
let i = elemIndex 'S' "QZAXSWED"
case i of
Just idx -> print idx
Nothing -> putStrLn "'S' not found in list"
There's also the function elemIndices that returns all the indices of an element in a list, but it's going to take longer to run since it has to scan the entire list every time.
Prelude Data.List> elemIndex 'S' "QAZXSWEDCVFRTGBNHYUJMKIOPL"
Just 4

In Haskell, how to construct lists and make substitution between it [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In Haskell, I am going to construct 2 list of chars; one is the 26 chars in the alphabet by the original(A,B,C,D,....Z), all in uppercase. And the other one is the same list but the order of letters is changed, like(B,H,A,I......S). And now I am also going to make a substitution between these two lists, such as when the input is B then returns H, C returns A.
Can anyone could help me out of this?
List literals in Haskell use square brackets [], first of all.
Second of all, I don't know the wording of your assignment, but free of constraint, I would use an association list instead of two lists. An association list is a list of pairs of the form [(a,b)]. The key operation on an association list lookup, defined in Data.List. Look at the type signature and see if you figure out what it does.
If you start with two lists, you can zip them up.
import Data.List
import Data.Maybe
codec = zip "ABCDEF..." "BHAI..." -- String ~ [Char]
encode = map $ flip lookup codec
plainText = "The secret fox"
encodedText = encode plainText
Notice that this gives you a list of [Maybe Char]. I leave it to you to figure out how to extract the chars, since it's actually a design choice. (Do you want to just omit characters that don't show up in the codex, or insert a '!' or something so that the user knows data has been lost? Lots of options here. Notice how the Maybe monad is forcing you to explicitly handle this case).
Assume that the modified alphabet is:
"ORXBMDTCIGJYKAVLSWFNUQEHZP"
The code should look like:
import Data.List
import Data.Maybe
alphabet = ['A'..'Z']
mapping = zip alphabet "ORXBMDTCIGJYKAVLSWFNUQEHZP"
according letter = (snd . fromJust . (find (\t -> f t letter))) mapping
where
f (x, y) letter = x == letter
main = print $ according 'H'
mapping would create the correspondence between alphabet and a modified version. And according would return letter from a modified, based on the original one.