🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of bkhl

bkhl's solution

to Isogram in the Prolog Track

Published at Sep 29 2019 · 0 comments
Instructions
Test suite
Solution

Determine if a word or phrase is an isogram.

An isogram (also known as a "nonpattern word") is a word or phrase without a repeating letter, however spaces and hyphens are allowed to appear multiple times.

Examples of isograms:

  • lumberjacks
  • background
  • downstream
  • six-year-old

The word isograms, however, is not an isogram, because the s repeats.

Source

https://en.wikipedia.org/wiki/Isogram

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

isogram_tests.plt

pending :-
    current_prolog_flag(argv, ['--all'|_]).
pending :-
    write('\nA TEST IS PENDING!\n'),
    fail.

:- begin_tests(isogram).

    test(empty_string, condition(true)) :-
      isogram("").

    test(only_lower_case_characters, condition(pending)) :-
      isogram("isogram").

    test(one_duplicated_character, [fail, condition(pending)]) :-
      isogram("eleven").

    test(longest_reported_english_isogram, condition(pending)) :-
      isogram("subdermatoglyphic").

    test(duplicated_character_in_mixed_case, [fail, condition(pending)]) :-
      isogram("Alphabet").

    test(hypothetical_isogrammic_word_with_hyphen, condition(pending)) :-
      isogram("thumbscrew-japingly").

    test(duplicated_non_letter_character, condition(pending)) :-
      isogram("Hjelmqvist-Gryb-Zock-Pfund-Wax").

    test(name_that_is_an_isogram, condition(pending)) :-
      isogram("Emily Jung Schwartzkopf").

    test(duplicated_character_in_the_middle, [fail, condition(pending)]) :-
      isogram("accentor").

    test(largest_pure_letter_characters_isogram, condition(pending)) :-
      isogram("AbcdEfghIjkLMNopQrStUvWxyZ.").

:- end_tests(isogram).
isogram(String) :-
    string_lower(String, LowercaseString),
    string_to_list(LowercaseString, List),
    include(alphabetic, List, LetterBag),
    sort(LetterBag, LetterSet),
    length(LetterBag, X),
    length(LetterSet, X).

alphabetic(C) :- char_type(C, alpha).

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?