# Friday code drop: Transforming properly between atom, string, codelist and charlist

**URL:** <https://swi-prolog.discourse.group/t/friday-code-drop-transforming-properly-between-atom-string-codelist-and-charlist/4056>\
**Category:** Announce\
**Created:** [June 11, 2021, 3:49pm UTC](https://swi-prolog.discourse.group/t/friday-code-drop-transforming-properly-between-atom-string-codelist-and-charlist/4056 "2021-06-11T15:49:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dtonhofer](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/dtonhofer/32/902_2.png) [@dtonhofer](https://swi-prolog.discourse.group/u/dtonhofer)\
**Post date:** [June 11, 2021, 3:49pm UTC](https://swi-prolog.discourse.group/t/friday-code-drop-transforming-properly-between-atom-string-codelist-and-charlist/4056/1 "2021-06-11T15:49:24Z")

</div>

While cleaning up my “dict prettyprinter”, a little solution to the problem of

- [`atom_string/2`](https://eu.swi-prolog.org/pldoc/doc_for?object=atom_string/2),

on the one hand, and

- [`atom_codes/2`](https://eu.swi-prolog.org/pldoc/doc_for?object=atom_codes/2)
- [`string_chars/2`](https://eu.swi-prolog.org/pldoc/doc_for?object=string_chars/2),
- [`string_codes/2`](https://eu.swi-prolog.org/pldoc/doc_for?object=string_codes/2).

on the other hand,

which are non-logical because they “accept a lot” but provide “at most one solution”.

## Direct

**The [README and code, including plunit test code](https://github.com/dtonhofer/prolog_code/blob/2c672b349e45e4797513c96f9bf437994625298e/unpacked/onepointfour_basics/README_stringy_morph.md)**

Currently a bit hard to install as you have to install dependencies, but the plan is to package all that ASAP.

## Explainer

A new predicate:

**`stringy_morph(StringyA,StringyB,TypeOfStringyA,TypeOfStringB)`**

which takes type information to cleanly transform between _string_ and _atom_.

**Example**

I want a _string_ at argument place 2, `StringyB` . I don’t care to be told about the type of argument 1, so I provide a `_` at argument place 3:

```prolog
?- stringy_morph(hello,StringyB,_,string).
StringyB = "hello".

```

If I state that the type of argument 1 is a _string_ , I get told otherwise:

```prolog
?- stringy_morph(hello,StringyB,string,string).
false.

```

Because it’s an _atom_ :

```prolog
?- stringy_morph(hello,StringyB,atom,string).
StringyB = "hello".

```

If I am lax in specifying the wanted output type, I get two solutions:

```prolog
?- stringy_morph(hello,StringyB,_,Whatever).
StringyB = hello, Whatever = atom ;
StringyB = "hello", Whatever = string.

```

Of course, one can _accept_ a pair of arguments:

```prolog
?- stringy_morph(hello,"string",atom,string).
true.

```

Or query their type, as long as they can be morphed from one to the other:

```prolog
?- stringy_morph(hello,"hello",TypeA,TypeB).
TypeA = atom,
TypeB = string.

```

Similarly a new predicate:

**`stringy_charylist_morph(Stringy,Charylist,StringyType,CharylistType)`**

which behaves likewise for transformations between atom/string and list of code/char.
