Does SML supports the nested list? - list

the Nested List can exist in Scheme, but is it legal to use nested-list in SML? or we can only use simple list in SML?
and if legal,
1) how to check wether the two input list have the same list structure. algorithm the atoms in the list are not equal.
2) Whatever the deep of the input list, how to delete all the atoms in the nested-list that equals to the input value: a. should use the original list and not create a new list.

There's no problem in having nested lists in Standard ML. For an example:
val foo = [ [1, 2, 3], [4, 5], [6] ]
is an example of an int list list, i.e., a list of lists of integers.
As for your additional questions.
1
If by same structure you mean whether the sublists contain the same number of elements, i.e, you want
val bar = [ [34, 4, 6], [2, 78], [22] ]
val baz = [ [1], [4, 6, 2], [3, 6] ]
val cmp_foo_bar = structureEq (foo, bar) (* gives true, since the lengths of the sublists match up *)
val cmp_foo_baz = structureEq (foo, baz) (* gives false, since they don't *)
Then you can simply make a recursive function on the lists, that compares the length of each sublist in turn.
Note, if the lists are nested more than once, you'll need a function for each level. (i.e., one for 'a list lists, one for 'a list list lists, etc.
2
You cannot make a function that "however deep the input list" does something to the elements in the list. The type system will not let you do this. This is similar to how you cannot make the following list:
val illegal_list = [ [1, 2], [ [1, 4], [2, 3] ] ]
This is due to a list only being allowed to contain one type of elements, so if you have an 'a list list, each element in the list must be an 'a list. You cannot have 'as directly.
You'll have to settle on how nested the lists are, and make a function specific to that depth.

There is no problem with nesting lists in SML, e.g. [[1, 2], [3, 4]] works just fine.
However, I suspect you actually mean something more general, namely the ability to nest "lists" in heterogeneous ways: [[1, [3]], 2]. This is not legal as such in SML. However, this is because such a thing is not really a list, it is a tree.
You can define trees easily as well, but you need a more general type definition than the one for list:
datatype 'a tree = L of 'a | T of 'a tree list
Then T[T[L 1, T[L 3]], L 2] is a representation of the "list" above. A function for computing the depth (or height) of such a tree looks like
fun depth (L _) = 0
| depth (T ts) = 1 + max (List.map depth ts)
where max needs to be defined in the obvious manner.

Related

Shifting a list N times using Prolog [] issue :- Exercise 3.6 “ Prolog Programming for Artificial Intelligence” by Ivan Btrako

I want to create a predicate shift(List1,List2) where List2 is List1 shifted rotationally by one element to the left.
Example:
?- shift([1,2,3,4],L1), shift(L1,L2), shift(L2,L3).
L1 = [2, 3, 4, 1],
L2 = [3, 4, 1, 2],
L3 = [4, 1, 2, 3].
I created the predicate
conc([],L,L).
conc([X|T],L2,[X|T1]) :-
conc(T,L2,T1).
shift([H|T],L2) :-
conc(T,H,L2).
However, it's wrong & I don't understand why... It works only if the H is surrounded by [H].
shift([H|T],L2) :-
conc(T,[H],L2).
The predicate without the [ ] only works with 1 shift & it outputs the following:
?- shift([1,2,3,4],L1).
L1 = [2, 3, 4|1].
Obviously, there's a basic concept I'm confused with, but I can't seem to pin-point what's wrong alone. I'm new at Prolog, so any help would be appreciated.
It works only if the H is surrounded by [H]
As I noted before, conc/3 is really the standard predicate append/3 which takes in two list and creates a third list.
append/3 says:
append(?List1, ?List2, ?List1AndList2)
List1AndList2 is the concatenation of List1 and List2
A single term is not a list, e.g. 1 is not a list, but [1] is a list.
A list starts with [ and ends with ].
This is the empty list: []
A list can have one item: [a]
or more than one item: [a,b] and so on. Notice how they always have square brackets.
When you use conc/3 or append/3 all three values have to be list, so even if you want to concatenate or append a single item you have to convert it to a list first by surrounding it with [] to turn it into a list. So the single item 1 is converted to a list as [1].

Haskell recurse and add to a list

I'm a Haskell beginner,
I have a function
func :: Num a => [a] -> [a]
func [] = []
func (x:xs) = x + func xs
Each recursion I want to append the value to a list for my output. This function will sum consecutive indexes in a list so that the input [1, 2, 3, 4] produces [1, 3, 6, 10].
How do I append the value generated each time to my list?
Your problem here isn't how to append, but rather how to calculate the value in the first place. Each item needs to be substituted with a sum of itself with all the items preceding it.
Here is one way to do it:
Prelude> func (x:xs) = x:map (+ x) (func xs); func [] = []
Prelude> func [1, 2, 3, 4]
[1,3,6,10]
How does this work? We're given a list that starts with the element x and has the remaining elements xs. We want to increment every item in xs by x, after recursively applying the algorithm to xs.
This is what x:map (+ x) (func xs) does. It reads as "prepend x to the result of mapping every element in func xs through an increment by x".
E.g. for [1, 2, 3, 4], we want 1 to be added to every member of the result of recursively applying the algorithm to [2, 3, 4], then prepended. For [2, 3, 4] we want 2 to be ... to [3, 4]. And so on, until eventually for [4] we want 4 to be added and prepended to the result of applying the algorithm to [].
This is where our base case (func [] = []) kicks in: the algorithm is defined so that it returns an empty list unchanged. Hence func [4] is [4], func [3, 4] is [3, 7], and you keep incrementing and prepending until you get [1,3,6,10].
I think in this particular case, you could use scanl1 like:
scanl1 (+) [1,2,3,4] -- [1,3,6,10]
When iterating over lists, we often use folds, which is a way of reducing the list to a particular value.
There's also another type of operation, which is a fold that collects all results along the way, and that's called a scan (from the docs):
scanl = scanlGo
where
scanlGo :: (b -> a -> b) -> b -> [a] -> [b]
scanlGo f q ls = q : (case ls of
[] -> []
x:xs -> scanlGo f (f q x) xs)
So the scan takes three arguments: a function that takes two values and returns a value, a starter value, and a list of values.
The scan will then return a list.
Thus, what you need is a function that takes two values and returns something of the same type as the first (it's okay if both are the same). Binary addition would work here: +.
You also need a value to start off with (the b, which is the second argument to our function), and 0 is the identity for integer addition, so we should use that.
Finally, we pass your list to get the result.
Try to figure out how to write you function as a fold and then as a scan and you will discover the answer.

Append Maybe to List in Elm

I have a List a and a Maybe a. I want to append the maybe value if it is Just a but do nothing if it is Nothing.
This is what I am currently using:
aList ++ case maybeValue of
Just value ->
[ value ]
Nothing ->
[]
Is there a better (more idiomatic) way of doing this?
Note that prepending is fine too if there is a cleaner way of doing that instead. The list order does not matter.
From Chad's suggestion that prepending is cheaper:
prependMaybe : List a -> Maybe a -> List a
prependMaybe list maybe =
case maybe of
Just value ->
value :: list
Nothing ->
list
I think you can use Maybe.map List.singleton yourMaybe |> Maybe.withDefault [].
Here you have a complete example:
appendMaybe : List a -> Maybe a -> List a
appendMaybe list maybe =
Maybe.map List.singleton maybe
|> Maybe.withDefault []
|> (++) list
You can try it on Ellie
If you're going for conciseness, you could use Maybe.Extra.unwrap from the elm-community/maybe-extra package:
import Maybe.Extra exposing (unwrap)
consMaybe : List a -> Maybe a -> List a
consMaybe list =
unwrap list (flip (::) list)
appendMaybe : List a -> Maybe a -> List a
appendMaybe list =
unwrap list ((++) list << List.singleton)
If you really want to go crazy, you can create your own infix operators:
infixr 5 ::?
(::?) = flip consMaybe
infixr 5 ++?
(++?) = appendMaybe
This allows the following:
Nothing ::? [2, 3, 4] == [2, 3, 4]
Just 1 ::? [2, 3, 4] == [1, 2, 3, 4]
[2, 3, 4] ++? Nothing == [2, 3, 4]
[2, 3, 4] ++? Just 5 == [2, 3, 4, 5]
Now, whether the infix versions are idiomatic Elm, that's up for debate. If it's something you use a lot, perhaps it's worth it, but most Elm guides urge you to avoid infix operators because they hinder discoverability.
In the end, your original example has the benefit of being readable and probably more readily understandable, since fewer people will be familiar with unwrap. The only suggestion would be that if order truly doesn't matter, then prepending an item to a list is going to be faster than concatenating lists.

PROLOG - Change elements in a list

I need to change elements in a list, I have the following code:
change_aux(_,_,[],[]).
change_aux(X,Y,[X|T],[Y|S]):-!,change_aux(X,Y,T,S).
change_aux(X,Y,[Z|T],[Z|S]):-change_aux(X,Y,T,S).
flatten2([], []) :- !.
flatten2([L|Ls], FlatL) :-
!,
flatten2(L, NewL),
flatten2(Ls, NewLs),
append(NewL, NewLs, FlatL).
flatten2(L, [L]).
change(X,Y,[X1|Y1],[X2,Y2]):-
flatten([X1|Y1],L),
change_aux(X,Y,L,[X2|Y2]).
Input: change(2,5,[1,[2,[3,2],1]],R).
Print: R = [1, [5, 3, 5, 1]] .
But I need R to be printed like this: R = [1,[5,[3,5],1]]
Could you help me, please?
There are some problems in the code above like in definition change(X,Y,[X1|Y1],[X2,Y2]):- I don't think that the output list should always consists of two elements. Besides that the change_aux predicate needs some work since now it's just traversing the list and not building the nested output list. You could try something that would build recursively the nested levels of the list like:
change(_,_,[],[]).
change(X,Y,[H|T],[H|T1]):- \+is_list(H),dif(H,X),change(X,Y,T,T1).
change(X,Y,[X|T],[Y|T1]):- change(X,Y,T,T1).
change(X,Y,[H|T],[L|T1]):- is_list(H),change(X,Y,H,L),change(X,Y,T,T1).
Note that in the above predicate there is no need to use flatten/2 predicate since we take advantage of the nested levels of input list to build output list.
Example:
?- change(2,5,[1,[2,[3,2],1]],R).
R = [1, [5, [3, 5], 1]] ;
false.

Appending lists into a list of lists in Haskell?

All I've been able to find in the documentation that are relevant are ++ and concat.
I thought at first doing the following would give me what I wanted:
[1, 3, 4] ++ [4, 5, 6]
but as you know that just gives [1, 2, 3, 4, 5, 6].
What would I need to do to take in [1, 2, 3] and [4, 5, 6] and get out [[1, 2, 3], [4, 5, 6]]?
As mentioned in comments, a function to take two lists and combine them into a new list can be defined as:
combine :: [a] -> [a] -> [[a]]
combine xs ys = [xs,ys]
This function can't be applied multiple times to create a list of an arbitrary number of lists. Such a function would take a single list and a list of lists and it would add the single list to the list of lists, so it would have type:
push :: [a] -> [[a]] -> [[a]]
This is just (:), though:
push = (:)
As also mentioned in the comments, the value [x,y] can also be written as x : y : [].1 Since both cases can be done with (:), I would guess that what you really want to use is (:), sometimes consing onto [] and sometimes onto a non-empty list.
1 In fact, [x,y] is just syntactic sugar for x:y:[].
think about what the (++) operator does: it concatenates Lists, it does not construct them. This is how it concatenates text Strings into a new String (and not a list of Strings), since Strings are lists of Chars. to construct a new List out of Lists you use (:) like so:
[1,2,3]:[4,5,6]:[]
where youre adding each list as an element of a new list.