Skip to content

Instantly share code, notes, and snippets.

View k3ut0i's full-sized avatar
🏠
Working from home

Rajashekar R M k3ut0i

🏠
Working from home
View GitHub Profile
@k3ut0i
k3ut0i / l2n.prolog
Created October 28, 2021 11:26
convert list of digits to number and vice-versa
:- use_module(library(clpfd)).
l2n_1([], Acc, Acc).
l2n_1([X|Xs], Acc, N) :-
Acc1 is 10*Acc+X,
l2n_1(Xs, Acc1, N).
%?- l2n_1([1, 2, 3], 0, X).
%@ X = 123.
@k3ut0i
k3ut0i / combinations.prolog
Created October 27, 2021 08:58
combinations of elements from a list
cons(X, Y, [X|Y]).
comb(0, _, [[]]).
comb(N, Xs, []) :- length(Xs, N1), N > N1.
comb(N, [X|Xs], Ys) :-
N > 0,
N1 is N - 1,
comb(N, Xs, Ys1),
comb(N1, Xs, Ys2),
maplist(cons(X), Ys2, Ys3),
@k3ut0i
k3ut0i / knights.prolog
Created October 20, 2021 12:54
knights knaves puzzle gnu-prolog
% -*- prolog-system: gnu -*-
q3(A, B, C) :-
fd_cardinality([A, B, C], Knights),
A #\<=> Knights,
B #<=> Knights,
fd_labeling([A, B, C]).
@k3ut0i
k3ut0i / so.prolog
Created October 15, 2021 13:01
catching errors in prolog
loadFileSafe(F, Status) :-
catch(load_files(F), error(existence_error(source_sink, F), _), Status = failed).
catchFileErrors(G, F) :-
catch(G, error(existence_error(source_sink, F), _), true).
safeLoadConfig(F) :-
catch(load_files(F),
error(existence_error(source_sink, F), _),
@k3ut0i
k3ut0i / so.hs
Created October 3, 2021 14:51
very declarative list comprehensions
filterMap :: (a -> Maybe b) -> [a] -> [b]
filterMap _ [] = []
filterMap f (x:xs) = case f x of
Just y -> y : filterMap f xs
Nothing -> filterMap f xs
assoc :: Eq b => [(a, b)] -> [(b, c)] -> [(a, c)]
assoc _ [] = []
assoc xs ((yb, yc):ys) = filterMap (f yb yc) xs ++ assoc xs ys
where
@k3ut0i
k3ut0i / serv.pl
Created October 1, 2021 13:41
simple mojo http server
use strict;
use warnings;
use feature qw(signatures);
no warnings qw(experimental::signatures);
use DateTime;
use Mojo::Server::Daemon;
my $daemon = Mojo::Server::Daemon->new(listen => ['http://*:8080']);
sub req ($d, $t) {
@k3ut0i
k3ut0i / flip-adjacent.lisp
Created October 1, 2021 08:30
tail call comparision
(defun flip-adjacent (lst)
(cond
((null lst) lst)
((null (cdr lst)) lst)
(t (destructuring-bind (a b . r) lst
(cons b (cons a (flip-adjacent r)))))))
(defun flip-adjacent-2 (lst acc)
(cond
((null lst) (reverse acc))
@k3ut0i
k3ut0i / so.prolog
Created September 28, 2021 10:24
A simple FD example
subject(t1, math).
subject(t2, math).
subject(t2, english).
subject(t3, sports).
subject(t3, art).
subject(t3, english).
subject(t4, math).
subject(t4, art).
subject(t5, sports).
subject(t5, math).
@k3ut0i
k3ut0i / church-nat.hs
Created July 2, 2021 15:42
Church encoding
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}
module ChurchNat () where
type Church a = (a -> a) -> a -> a
-- instance Functor Church where
-- fmap f n = undefined
suc :: Church a -> Church a
@k3ut0i
k3ut0i / basics.ps
Created January 3, 2021 15:26
simple squares and triangles in postscript
%!PS
/line {
moveto
lineto
stroke
} bind def
/++ {
1 add def %How to get the value of a name in the stack?