How convert type of value in Visual Prolog 8?

652 Views Asked by At

I need convert string to ACII add 1 and convert back.

Next_letter = convert(char, convert(integer, Letter) + 1)

But error:

main.pro(19,48) error c504 : The expression has type 'main::letter', which is incompatible with the type '::integer'

Full program :

   /* программа prog2_8.pro */
/* генерации ряда букв а…d в столбик в порядке возрастания */

implement main
    open core, console

domains
    letter = string.

class predicates
    write_letter : (letter).
clauses
    write_letter("e").

    write_letter(Letter) :-
        Letter < "e",
        write("   ", Letter),
        nl,
        Next_letter = convert(char, convert(integer, Letter) + 1),
        write_letter(Next_letter).

    run() :-
        write("letters:"),
        nl,
        write_letter("a"),
        fail().

    run() :-
        nl,
        write("Press Enter"),
        _ = readchar(),
        succeed().

end implement main

goal
    console::runUtf8(main::run).
1

There are 1 best solutions below

0
Rafalon On

Try the following using char_int/2:

char_int(Letter, IntLetter),        % Letter = 'a' -> IntLetter = 97
NextIntLetter = IntLetter + 1,      % NextIntLetter = 98
char_int(NextLetter, NextIntLetter) % NextLetter = 'b'

And here NextLetter should be what you need


From VIP documentation

char_int(CHAR CharArg, INTEGER IntArg)

Flow patterns (i, o), (o, i), (i, i)

Convert between characters and their ASCII values

Remarks

(i, o) Binds IntArg to the ASCII code for CharArg.
(o, i) Binds CharArg to the character having the ASCII code specified by IntArg (notice that it takes into account only the lower byte of an integer value).
(i, i) Succeeds if IntArg is bound to the ASCII code for CharArg; fails if it is not.

Fail

See remarks for (i, i).

Errors

No errors.

Example

To test this example use "Project | Test Goal" in Visual Prolog VDE.

GOAL:
    char_int('a',X) 
    X=97 
    1 Solution

GOAL:
    char_int(X,97) 
    X=a 
    1 Solution

GOAL:
    char_int('a',97) 
    Yes

GOAL:
    char_int('a',197) 
    No

Edit

Your problem is that a string can not be converted in its ASCII value as it is a set of characters. You have to replace letter = string with letter = char and " (around your characters) with '.