What does the Haskell list syntax without commas mean? - list

I'm looking through some Haskell code and I saw an expression with the following shape:
[A B C D]
I know that a list in Haskell would have commas between the elements. What does this notation mean? Could someone point me to the appropriate documentation?

4 elements separated by only space means the same thing inside list brackets as it does outside them: A is applied to B and then to C and then to D.

Related

Is this a correct characterisation of the behaviours of square brackets in Scheme, Racket, Common Lisp, and Clojure?

I've recently done some digging. Due to my lack of Lisp experience, I want to verify what I've found. Is this a correct characterisation of the behaviours of square brackets in Scheme, Racket, Common Lisp, and Clojure?
R5RS and R7RS Scheme: Square brackets are reserved in the spec "for possible future extensions to the language" (page 6 for R5RS and page 9 for R7RS), but implementations tend to implement square brackets as R6RS Scheme does below. Racket is one such example, even having community conventions for where to prefer square brackets.
R6RS Scheme: "Matched square brackets can be used synonymously with parentheses". In other words, square brackets are valid brackets as long as they match. Alternative source.
Common Lisp: Square brackets are reserved so the user can give them meaning via reader macros. They do not have meaning within the standard language, except inside FORMAT control strings.
Clojure: Square brackets for vectors and parentheses for lists. These are not the same thing.
I think for Scheme, you have it right.
In Common Lisp, the standard is not CLtL2, but the ANSI standard, which for all intents and purposes is the same as the CLHS (e. g. at https://clhs.lisp.se). The reader behaviour is defined in section 2. Square brackets by default (i. e. in standard syntax) are constituent characters, so they may be used without special escaping in symbol names. For example, [, [], ][, APPLE-][, >][<, [[[, etc. are all valid symbol names that can be used without escaping. These characters are, however, explicitly reserved for the programmer to use in their own readtables. Several libraries use this, e. g. CLSQL for SQL literals.
In Clojure, square brackets denote vectors, which is a separate kind of collection from e. g. lists. The evaluation of a literal vector in code is to construct a new vector of the evaluated forms inside. Example: [a 'b (+ a b)] evaluates to a new vector of the value of a, the symbol named b, and the sum of the values of a and b. Macros and other special forms generally use vectors for syntax parts that are not to be evaluated as a function call. Examples: (defn foo [a b c] …) — a vector of symbols to be bound as formal parameters in the body of the function definition. (let [a 1 b (+ a forble)] …) — a lexical binding for a and b.

Prolog Insert the number in the list by the tail

How can I build a predicate in prolog that receives a number and a list, I must insert the number in the list by the tail
I tried inserting the number in the list by the head: insert(H,[P|Q],[H,P|Q]). and it works, but how can I do it by the tail?
Simply use append/3 like this:
?- append([a,b,c,d],[x],List).
List = [a,b,c,d,x].
Inserting at the tail can be done with a two-part recursive rule:
When the list is empty, unify the result to a single-element list with the item being inserted
When the list is not empty, unify the result to a head followed by the result of inserting into the tail of a tail.
English description is much longer than its Prolog equivalent:
ins_tail([], N, [N]).
ins_tail([H|T], N, [H|R]) :- ins_tail(T, N, R).
Demo.
Nobody talked about difference lists yet.
Difference lists
Difference lists are denoted L-E, which is just a convenient notation for a couple made of a list L whose last cons-cell has E for its tail:
L = [ V1, ..., Vn | E]
The empty difference list is E-E, with E a variable. You unify E whenever you want to refine the list.
For example, if you want to add an element X, you can unify as follows:
E = [X|F]
And then, L-F is the new list. Likewise, you can append lists in constant time. If you unify F with a "normal" list, in particular [], you close your open-ended list. During all operations, you retain a reference to the whole list through L. Of course, you can still add elements in front of L with the ususal [W1, ..., Wm |L]-E notation.
Whether or not you need difference lists is another question. They are intereseting if adding an element at the end is effectively a common operation for you and if you are manipulating large lists.
Definite clause grammars
DCG are a convenient way of writing grammar rules in Prolog. They are typically implemented as reader macros, translating --> forms into actual predicates. Since the purpose of grammars is to build structures during parsing (a.k.a. productions), the translation to actual predicates involves difference lists. So, even though both concepts are theoretically unrelated, difference lists are generally the building material for DCGs.
The example on wikipedia starts with:
sentence --> noun_phrase, verb_phrase.
... which gets translated as:
sentence(S1,S3) :- noun_phrase(S1,S2), verb_phrase(S2,S3).
A lot of "plumbing" is provided by the syntax (a little like monads). The object being "built" by sentence/2 is S1, which is built from different parts joined together by the predicates. S1 is passed down to noun_phrase, which builds/extends it as necessary and "returns" S2, which can be seen as "whatever extends S1". This value is passed to verb_phrase which updates it and gives S3, a.k.a. whatever extends S2. S3 is an argument of sentence, because it is also "whatever extends S1", given the rule we have. But, this is Prolog, so S1, S2 and S3 are not necessarly inputs or outputs, they are unified during the whole process, during which backtracking takes place too (you can parse ambiguous grammars). They are eventually unified with lists.
Difference lists come to play when we encounter lists on the right-hand side of the arrow:
det --> [the].
The above rule is translated as:
det([the|X], X).
That means that det/2 unifies its first argument with an open-ended list which tail is X; other rules will unify X. Generally, you find epsilon rules which are associated with [].
All the above is done with macros, and a typical error is to try to call an auxiliary predicate on your data, which fails because the transformation add two arguments (a call to helper(X) is in fact a call to helper(X,V,W)). You must enclose actual bodies between braces { ... } to avoid treating prediates as rules.
Here is an another option.
insert(N,[],[N]).
insert(N,[H|T],[H|Q]) :- conc([H|T],[N],[H|Q]).
conc([],L,L).
conc([H|T],L,[H|Q]) :- conc(T,L,Q).

How to concatenate list values in OCaml

If I have a function
let rec function n =
if n<0 then []
else n-2 # function n-2 ;;
I get an error saying that the expression function n-2 is a list of int but it is expecting an int.
How do I concatenate the values to return all the n-2 values above zero as a list?
I cannot use the List module to fold.
Thanks
Your title asks how to concatenate lists, but your question seems rather different.
To concatenate lists, you can use the # operator. In many cases, code that depends on this operator is slower than it needs to be (something to keep in mind for later :-).
Here are some things I see wrong with the code you give:
a. You can't name a function function, because function is a keyword in OCaml.
b. If you use the # operator, you should have lists on both sides of it. As near as I can see, the thing on the left in your code is not a list.
c. Function calls have higher precedence than infix operators. So myfun n - 2 is parsed as (myfun n) - 2. You probably want something closer to myfun (n - 2).
Even with these changes, your code seems to generate a list of integers that are 2 apart, which isn't what you say you want. However, I can't understand what the function is actually supposed to return.
It seems like you are not concatenating lists, but concatenating ints instead. This is done by the :: operator. So your code would look like:
else (n-2)::(fun (n-2))
Although I could see this function possibly not producing the desired output if you put in negative numbers. For example if you pass through n = 1, n-2 will evaluate to -1 which is less than zero.

Is the language L={words without the substring 'bb' } regular?

L = {words such that the substring 'bb' is not present in in it
Given that the alphabet is A = {a,b}, is this language regular? If so, is there a regular expression that represents it?
Yes, this language is regular. Since this looks like homework, here's a hint: if the string bb isn't present, then the string consists of lots of blocks of strings of the form a* or a*b. Try seeing how to assemble the solution from this starting point.
EDIT: If this isn't a homework problem, here's one possible solution:
(a*(ba+)*b?)?
The idea is to decompose the string into a lot of long sequences of as with some b's interspersed in-between them. The first block of a's is at the front. Then, we repeatedly place down a b, at least one a, and then any number of additional as. Finally, we may optionally have one b at the end. As an alternative, we could have the empty string, so the entire thing is guarded by a ?.
Hope this helps!

How can I search a list with the underscore variable? (Prolog) [duplicate]

This question already has an answer here:
"Or" procedure in prolog
(1 answer)
Closed 8 years ago.
here's my problem I receive various lists with the underscore variable in them (for example: [ _, _, A, _, _] or [ _,A, B, _, _]), and I need to search those lists for the values that matter (in this case A or A and B)
Is it even possible or am I trying to solve this the wrong way.
For better context: I'm trying to make a cluedo type program in Prolog, you have your clues, then you receive clues from other players and try to find the murderer, but I want one list for every suspect ( like this [name,age,weapon, personal item]), so as you can see every time you get a clue like a weapon I receive a list like this [ _, _, knife, _]
if anyone could help i would appreciate
best regards
The way to check whether a variable is still free is with the ISO predicates var/1 and nonvar/1, which are true if their argument is a free variable or instantiated.
So, if you have a list with some variables bound, you can say:
nonvar_member(M, List) :- member(M, List), nonvar(M).
This predicate will enumerate (unify with M through backtracking) the elements of List which are not free variables:
?- nonvar_member(X, [2,_,_,3,A,1,_]).
X = 2 ;
X = 3 ;
X = 1 ;
false.
But altogether there are many examples around (also on Stackoverflow) of using Prolog for solving puzzles based on clues, so try to look at those first.