Skip to content

Instantly share code, notes, and snippets.

So you've got the two bugs: your
> isprimerec _ 1 = False
> isprimerec n t = if (n `rem` t) == 0 then False else isprimerec n (n-1)
should have been
> isprimerec _ 1 = True
> isprimerec n t = if (n `rem` t) == 0 then False else isprimerec n (t-1)
@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:
;; http://stackoverflow.com/a/20691734/849891 answered Dec 19 '13 at 21:10
;; by Joshua Taylor
Although there's already an accepted answer, I thought you might appreciate a Scheme
translation of the [Sheep Trick from _The Pitmanual_][1]. Your code is actually
quite similar to it already. Scheme does support `do` loops, but they're not
particularly idiomatic, whereas named `let`s are much more common, so I've used the
latter in this code. As you've noted, choosing the first element as the pivot cause
perfomance problems if the list is already sorted. Since you have to traverse the
list on each iteration, there might be some clever thing you could do to pick the
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:

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

;;;; 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
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 )
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..])