0

To concatenate two strings resulting in other, I know the predicate string_concat/3. However, someone know how implement a predicate who does the same?

Kijewski
  • 25,517
  • 12
  • 101
  • 143
rwehresmann
  • 1,108
  • 2
  • 11
  • 27
  • 1
    I just learned that SWI-Prolog now has a [string type](http://www.swi-prolog.org/pldoc/man?section=strings). – Kijewski Sep 02 '15 at 22:54
  • 2
    @Kay: SWI always had this type, but it was used for internal purposes only. Now, it is more openly advertised, but in an - unfortunately - non-conforming manner. – false Sep 03 '15 at 12:37
  • 1
    @false: I think it's an anti-feature: `?- append("Hello ", "World", "Hello World").` `false.` – Kijewski Sep 03 '15 at 12:51
  • 2
    @Kay: It all depends on the value of [`double_quotes`](http://stackoverflow.com/a/8269897/772868). – false Sep 03 '15 at 12:56
  • 2
    @false: I guess for most purposes it is not really important if `"abc" = [97,98,99]` or `"abc" = [a,b,c]`. But `"" \= []` is not very user/programmer friendly IMO. – Kijewski Sep 03 '15 at 13:03

2 Answers2

0

You can turn strings into lists of codepoints using string_codes/2.

?- string_codes("Hello, StackOverflow!", L).
L = [72, 101, 108, 108, 111, 44, 32, 83, 116|...].

?- string_codes(S, [97,98,99]).
S = "abc".

If you want to implement string_concat/3 yourself, than you have to take care of the different cases:

  • Only the last argument is free.
  • Only the last argument is bound.
  • The last argument and one of the former arguments is bound.
  • All arguments are bound.

Just convert the input strings to lists of codepoints, use append/3 to concatenate the lists, and convert the result back into a string. The only thing you have to look out for is not to invoke string_concat/2 without an instantiated variable.

Kijewski
  • 25,517
  • 12
  • 101
  • 143
  • This is a "correct" but very misleading answer. The main reason for having strings in SWI-Prolog is to allow for efficient representation of text that _does not need_ to be broken down to code lists (or char lists). –  Sep 07 '15 at 11:21
0

I know it is frowned upon on Stack Overflow to point it out, but still: you are asking the wrong question!

The main reason for strings in SWI-Prolog is to allow for representing "text" that does not need to be represented as a proper Prolog list. Using a "string" for text is more space-efficient, and some operations on strings using the available predicates are more efficient than doing the same with code lists or atoms.

But writing your own predicate in native Prolog that manipulates an opaque data type implemented in the underlying language is a bit... unnecessary?

And make sure to read the section in the manual on Representing text.

  • 1
    Since 1975 resp 1978 (Prolog I, DECsystem 10 Prolog), strings were either list of characters (atoms of length 1), or lists of character codes. It's this change in SWI which is the problem. – false Sep 07 '15 at 17:29
  • @false I know how you feel about it; however, there are lists of characters in SWI-Prolog, as well as lists of codes. Your comment might be misinterpreted to mean that those have been abolished in favour of "strings". –  Sep 08 '15 at 06:23
  • 1
    For swish they have been abolished - you cannot change double_quotes. And generally double quotes are now used as the canonical representation for SWI-strings, which means you can no longer use them for something else. – false Sep 08 '15 at 07:12
  • The best you can do in SWI7 is: `set_prolog_flag(back_quotes, string)` and never change that. In this manner `write_canonical/1` will always write them with backquotes — regardless of the value for `double_quotes`. – false Sep 08 '15 at 17:57