Copy certain characters from a list to another on prolog

Nick Pampoukidis

So i have this code which copies everything from a list to another one. How should I modify it in order to copy, lets say the first two character.

$copy(L,R) :- 
    copy2(L,R).
copy2([X],[X]).
copy2([H|T1],[H|T2]) :-
    copy2(T1,T2).

example of what i want it to be: ?- copy([a,b,c,d,e,f],X,2). --> X = [a,b]

Shon

You can copy lists just with unification:

?- [a,b,c,d,e] = List.
List = [a, b, c, d, e].

?- [a,b,c,d,e] = [V,W,X,Y,Z].
V = a,
W = b,
X = c,
Y = d,
Z = e.

?- [a,b,c,d,e] = [V,W|Rest].
V = a,
W = b,
Rest = [c, d, e].

A predicate like the one you describe, copying the first N elements of a list, can be defined thus:

first_n(List, N, Xs) :-
    length(Xs, N),
    append(Xs _, List).

Which works like so:

?- first_n([a,b,c,d,e], 2, X).
X = [a, b].

There are a bunch of different ways to write a similar predicate. The way I have defined first_n/3, it will fail if N is larger than the length of List (this was pointed to out by @false in the comments). One could instead write an analog of the common function take, which will return List in its entirety in the event that N is greater than List's length:

take_n(N, List, Taken) :-
    ( length(List, M),
      N > M
    ->
      Taken = List
    ;
      length(Taken, N),
      append(Taken, _, List)
    ).

This answer was corrected (several times) under the guidance of @false's helpful criticism.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Copy certain characters from a list to another on prolog

From Dev

Prolog copy a specific letter from a list to another in the same place

From Dev

Moving certain characters from one file to another

From Dev

Copy List to another List, but excluding certain properties of List type

From Dev

Prolog - copy a piece of list

From Dev

Copy values from a file to a new file only after certain characters

From Dev

Copy certain files from one folder to another using python

From Dev

PHP - copy certain array from multidimensional array to another new array

From Dev

How to copy a certain word from one string to another in Java

From Dev

PHP - copy certain array from multidimensional array to another new array

From Dev

Copy only certain file types from a folder structure to another

From Dev

Translating a list to another list in prolog

From Dev

VBA - Copy first three characters from string into another cell

From Dev

How to read two characters at once from one string and copy into another

From Dev

copy files from C:\ to another location with four characters or less

From Dev

Copy row from Master List if Column D contains certain text

From Dev

Remove certain elements in one list based on condition from another list

From Dev

Creating a List from another List, filtered by certain indexes

From Dev

Copy certain directories to another directory

From Dev

Copy an object from a list into another object in the same list

From Dev

Copy the characters from the array

From Dev

Prolog - How to check if a list contains a certain item

From Dev

Comparing a list with lists in another list in Prolog

From Dev

Comparing a list with lists in another list in Prolog

From Dev

Copy all matching items from one list to another

From Dev

Copy values from one list to another without altering the reference in python

From Dev

Using LINQ to copy data from one list to another

From Dev

How to copy only new items from one list to another

From Dev

Copy Item from One static list to another and revert as well

Related Related

  1. 1

    Copy certain characters from a list to another on prolog

  2. 2

    Prolog copy a specific letter from a list to another in the same place

  3. 3

    Moving certain characters from one file to another

  4. 4

    Copy List to another List, but excluding certain properties of List type

  5. 5

    Prolog - copy a piece of list

  6. 6

    Copy values from a file to a new file only after certain characters

  7. 7

    Copy certain files from one folder to another using python

  8. 8

    PHP - copy certain array from multidimensional array to another new array

  9. 9

    How to copy a certain word from one string to another in Java

  10. 10

    PHP - copy certain array from multidimensional array to another new array

  11. 11

    Copy only certain file types from a folder structure to another

  12. 12

    Translating a list to another list in prolog

  13. 13

    VBA - Copy first three characters from string into another cell

  14. 14

    How to read two characters at once from one string and copy into another

  15. 15

    copy files from C:\ to another location with four characters or less

  16. 16

    Copy row from Master List if Column D contains certain text

  17. 17

    Remove certain elements in one list based on condition from another list

  18. 18

    Creating a List from another List, filtered by certain indexes

  19. 19

    Copy certain directories to another directory

  20. 20

    Copy an object from a list into another object in the same list

  21. 21

    Copy the characters from the array

  22. 22

    Prolog - How to check if a list contains a certain item

  23. 23

    Comparing a list with lists in another list in Prolog

  24. 24

    Comparing a list with lists in another list in Prolog

  25. 25

    Copy all matching items from one list to another

  26. 26

    Copy values from one list to another without altering the reference in python

  27. 27

    Using LINQ to copy data from one list to another

  28. 28

    How to copy only new items from one list to another

  29. 29

    Copy Item from One static list to another and revert as well

HotTag

Archive