Skip to content

Instantly share code, notes, and snippets.

@WillNess
WillNess / corec.pl
Last active September 10, 2018 13:50
/**
* Lazy lists represented as Closure (like plus(1)) that gives
* NextElt, NextClosure on call to call(Closure, NextElt, NextClosure) -- remarks and edits by wn
* cf. unfold :: (b -> (a, b)) -> b -> [a] -- for my personal readability
*
* Copyright 2018, XLOG Technologies GmbH, Switzerland
* Jekejeke Prolog 1.3.0 (a fast and small prolog interpreter)
*/
%% source:
even non-executable.
"fix $ map head . scanl (\\) [2..] . map (\p->[p,p+p..])" -- what is (\\)?..
"sv (p:xs) = p : sv (remove (multiplesOf p) xs)" -- what is remove?.. what is multiplesOf?..
<< https://twitter.com/yminsky/status/974411177731280896
Yaron Minsky @yminsky 19h19 hours ago
A technique that I think people don't use often enough, and that I don't use often enough:
;;;; http://wiki.c2.com/?SieveOfEratosthenesInManyProgrammingLanguages
;;;; Stream Implementation
(define (head s) (car s)) ;; _odd_ non-memoized streams,
(define (tail s) ((cdr s))) ;; per SRFI-41
(define-syntax s-cons
(syntax-rules () ((s-cons h t) (cons h (lambda () t)))))
;;;; Stream Utility Functions
import math
import sys # stackoverflow.com/questions/49149932/
# python-generator-and-setgenerator-get-different-results
# by stackoverflow.com/users/4658633/blaise-wang
import time
from mpi4py import MPI
import eratosthenes
ps = sv [2..]
where
sv (p:t) = [p] ++ sv [n | n <- t, rem n p > 0]
ps = sv [2..]
where
sv (p:t) = [p] ++ sv (t \\ [p, p+p..])

https://stackoverflow.com/questions/49136510/list-all-leaves-of-huffman-tree/49137936#49137936

On the "fundamentals" level, in Scheme we write functions to either construct and return a value, or for their side-effects – meaning, when they do something while we ignore their returned value.

What can a function do? A list can be altered in-place with set-car!, and grown ⁄ shrunk with set-cdr!, as an example. append does neither. It constructs new value, new list, from its arguments. But then we have to use this new value somehow, have to return it from our function. Just writing (append a b) does nothing, if it's not (in) the last expression in a function.

Your code is written in imperative style, and even that with the order of operations jumbled up. You meant

apply f args = eval [f, ...[[QUOTE, a] | a <- args]...] [] % McCarthy-original-LISP-paper
eval e a | atom e = head [v | [n, v] <- a, n == e]
| otherwise =
case e of
[QUOTE, x] -> x
[FUNCTION, x] -> [FUNARG, x, a]
[EQ, x, y] -> eval x a == eval y a
[CONS, x, y] -> [eval x a, ...eval y a...]
[CAR, x] -> head ( eval x a )
@WillNess
WillNess / mcc.erl
Last active February 28, 2018 00:31
apply f args = eval [f, ...[[QUOTE, a] | a <- args]...] [] % McCarthy-original-LISP-paper
eval e a | atom e = case [v | [n, v] <- a, n == e] of {[v, ...] -> v} % first matching name's value
| otherwise =
case e of
[QUOTE, x] -> x % "expression"
[FUNCTION, x] -> [FUNARG, x, a] % closure (new)
[ATOM, x] -> atom (eval x a)
[EQ, x, y] -> eval x a == eval y a
[CONS, x, y] -> [eval x a, ...eval y a...]