How to resolve the algorithm First-class functions step by step in the Prolog programming language
How to resolve the algorithm First-class functions step by step in the Prolog programming language
Table of Contents
Problem Statement
A language has first-class functions if it can do each of the following without recursively invoking a compiler or interpreter or otherwise metaprogramming:
Write a program to create an ordered collection A of functions of a real number. At least one function should be built-in and at least one should be user-defined; try using the sine, cosine, and cubing functions. Fill another collection B with the inverse of each function in A. Implement function composition as in Functional Composition. Finally, demonstrate that the result of applying the composition of each function in A and its inverse in B to a value, is the original value. (Within the limits of computational accuracy). (A solution need not actually call the collections "A" and "B". These names are only used in the preceding paragraph for clarity.)
First-class Numbers
Let's start with the solution:
Step by Step solution about How to resolve the algorithm First-class functions step by step in the Prolog programming language
Source code in the prolog programming language
:- use_module(library(lambda)).
compose(F,G, FG) :-
FG = \X^Z^(call(G,X,Y), call(F,Y,Z)).
cube(X, Y) :-
Y is X ** 3.
cube_root(X, Y) :-
Y is X ** (1/3).
first_class :-
L = [sin, cos, cube],
IL = [asin, acos, cube_root],
% we create the composed functions
maplist(compose, L, IL, Lst),
% we call the functions
maplist(call, Lst, [0.5,0.5,0.5], R),
% we display the results
maplist(writeln, R).
You may also check:How to resolve the algorithm Mouse position step by step in the Racket programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the min programming language
You may also check:How to resolve the algorithm Primality by trial division step by step in the ACL2 programming language
You may also check:How to resolve the algorithm Pinstripe/Display step by step in the Locomotive Basic programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Forth programming language