This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%This is a basic English-to-Prolog translator. It uses a | |
%pathfinding algorithm to find the meaning of each sentence. | |
%For example, output([a,is,greater,than,B],X) means X = (a > b). | |
%The input is an English phrase. The output is a Prolog expression. | |
output(Input,Output) :- | |
is_output(Output), | |
means(Input,Output). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
attr(A, [N-V | R]):- memberchk( N-X, A), X = V, attr(A, R). | |
attr(_, []). | |
color(A, B):- attr( A, [color-B]). | |
pigs( Pigs):- | |
length( Pigs,N), | |
N rem 2 =:= 1, Middle is N div 2, /* there _is_ a middle - list length is odd */ | |
nth0( Middle,Pigs,P1), attr( P1, [color-brown]), | |
member( P2, Pigs), attr( P2, [color-brown, eats-carrots]), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:- object(database). | |
:- public(rule/2). | |
nat(zero) :- true. | |
nat(s(X)) :- nat(X). | |
add(zero, Y, Y) :- true. | |
add(s(X), Y, s(Z)) :- | |
add(X, Y, Z). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%Since SWI-Prolog does not have a built-in implementation of closures, I wrote my own implementation here. | |
:- initialization(main). | |
:- set_prolog_flag('double_quotes','chars'). | |
main :- predicate_with_nested(1,C),writeln(C). | |
call_local(Definition,Params) :- | |
copy_term(Definition,(Params :- Body)), | |
call(Body). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% test predicate | |
test(Summary, Predicate) :- | |
string_concat('\n ', Summary, Buf), | |
string_concat(Buf, ' is running', Message), | |
write(Message), | |
call(Predicate), | |
write('\ndone.'). | |
% example | |
force(anakin, 9). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/gawk -f | |
# a simple interpreter for GWBASIC(subset) written in AWK | |
# following 'The AWK Programming Language' Chapter 6 | |
# @date June 20,2012 | |
# @link basic.sh https://gist.github.com/2959816 | |
# @link test.sh https://gist.github.com/2959877 | |
# TODO: | |
# gosub/return...ok | |
# if/else/elseif/endif...ok | |
# while/wend...ok |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/awk -f | |
# Experimental implementation of ifdef in awk | |
BEGIN { | |
depth = 0; | |
defined = ""; | |
printing = 1; | |
} | |
$1 ~ /#DEFINE/ { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env awk -f | |
# Given a C-like header file, this AWK script will generate a sequence | |
# of case statements for stringifying macro names. | |
# | |
# For example, if the input file looks like this: | |
# | |
# #define kIOReturnSuccess KERN_SUCCESS // OK | |
# #define kIOReturnError iokit_common_err(0x2bc) // general error | |
# #define kIOReturnNoMemory iokit_common_err(0x2bd) // can't allocate memory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: awk -f json-to-csv.awk values.json > output.csv | |
# Note: You may encounter issues w/ esacping values or missing/empty fields | |
BEGIN{ OFS=""; u=f=l=a=e=""; } | |
/"username"/{ u=$2 } | |
/"first_name"/{ f=$2 } | |
/"last_name"/{ l=$2 } | |
/"asset_type"/{ a=$2 } | |
/"email"/{ e=$2 } | |
{ if(e!="") { print u,f,l,a,e; e="" }} # add for any potentially empty lines |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/awk -f | |
# Source: http://c2.com/doc/expense/ | |
/^[A-Z]+[A-Z0-9]*$/ { | |
if (sums[$1] == "" || $1 == "SUM") { | |
sums[$1] = sum # Define Symbol | |
$1 = sum | |
sum = 0 | |
} |
NewerOlder