Created
May 15, 2015 17:35
-
-
Save Luiz-Monad/0c29158f1b1c7567849e to your computer and use it in GitHub Desktop.
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
#Creates a named function using the script block. | |
function Add-Function( | |
[string]$name, | |
[Parameter(ValueFromPipeline=$true)] | |
[ScriptBlock]$block) | |
{ | |
New-Item -Path "Function:" -Name "global:$name" -Value $block | |
} | |
#returns a function by name | |
function Get-Function( | |
[string]$name) | |
{ | |
$name = $name.Replace("?", "??"); | |
Get-Item -Path "Function:$name" | |
} | |
#Creates our symbol table | |
#the symbols will be exported to powershell | |
#function table | |
#our guidelines https://github.com/jasonhemann/microKanren/blob/master/microKanren.scm | |
function _makeSymTable() | |
{ | |
$table = @{ | |
gt = {param($a, $b) $a -gt $b}; | |
ge = {param($a, $b) $a -ge $b}; | |
lt = {param($a, $b) $a -lt $b}; | |
le = {param($a, $b) $a -le $b}; | |
eq = {param($a, $b) $a -eq $b}; | |
not = {param($a) -not $a}; | |
"eq?" = {param($a) $a -eq $b}; | |
"equal?" = {param($a) $a -eq $b}; | |
apply = {param($a) & $a $b}; | |
begin = {param($a) foreach($i in $a) { & $i } }; | |
car = {param($a) $a}; | |
cdr = {param() $args | Select-Object -Skip 1 }; | |
cons = {param($a, $b) @($a, $b)}; | |
define = {param($a, $b) { $b }.GetNewClosure() | Add-Function $a }; | |
map = {param($a, $b) foreach($i in $a) { & $b $i } }; | |
quote = {param() $args}; | |
if = {param($a,$b,$c) if($a) {& $b} else {& $c} }; | |
length = {param($a) $a.Length }; | |
list = {param($a) $a}; | |
"list?" = {param($a) $a -contains $b}; | |
append = {param($a, $b) $a + $b}; | |
add = {param($a, $b) $a + $b}; | |
sub = {param($a, $b) $a - $b}; | |
mul = {param($a, $b) $a * $b}; | |
div = {param($a, $b) $a / $b}; | |
abs = {param($a) [Math]::Abs($a)}; | |
max = {param($a) [Math]::Max($a, $b)}; | |
min = {param($a) [Math]::Min($a, $b)}; | |
"null?" = {param($a) $a -eq $null}; | |
"number?" = {param($a) $a -is [ValueType]}; | |
"procedure?" = {param($a) $a -is [ScriptBlock]}; | |
"symbol?" = {param($a) (Get-Function $a) -ne $null}; | |
} | |
$table.Keys | % { | |
$table[$_] | Add-Function $_ | |
} | |
} | |
#test program | |
_makeSymTable | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment