Skip to content

Instantly share code, notes, and snippets.

voila

  • Dunedin, NZ
Block or report user

Report or block voila

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@voila
voila / Holop
Created Mar 4, 2013
Higher-order list operations exercices
View Holop
#lang racket
;; http://matt.might.net/articles/higher-order-list-operations/
;; Rewrite abstract mapping and folding using matching.
(define (abs-map kons nil f lst)
(match lst
['() nil]
[(cons hd tl)
(kons (f hd) (abs-map kons nil f tl))]))
View prop_delete1.erl
%% For any integer X and any list of integers L,
%% if I delete X from L, then L should not contain X
prop_delete1() ->
?FORALL({X,L}, %% variables
{integer(), list(integer())}, %% generators
not lists:member(X, delete_all(X, L))). %% property
View delete_all1.erl
%% A delete_all() implementation
delete_all(X, L) ->
delete_all(X, L, []).
delete_all(_, [], Acc) ->
lists:reverse(Acc);
delete_all(X, [X|Rest], Acc) ->
lists:reverse(Acc) ++ Rest;
delete_all(X, [Y|Rest], Acc) ->
delete_all(X, Rest, [Y|Acc]).
View proper1.
16> proper:quickcheck(listdel:prop_delete1()).
....................................................................................................
OK: Passed 100 test(s).
true
View prop_delete2.erl
%% same property but we want to see how often X belongs to L
prop_delete2() ->
?FORALL({X,L},
{integer(), list(integer())},
collect(lists:member(X, L), %% does X belongs to L ?
not lists:member(X, delete_all(X, L)))).
View proper2
17> proper:quickcheck(listdel:prop_delete2()).
....................................................................................................
OK: Passed 100 test(s).
82% false
18% true
true
View prop_delete3.erl
%% Pick X from L's elements
prop_delete3() ->
?FORALL(L,
list(integer()),
?IMPLIES(L /= [], %% rejects empty lists
?FORALL(X, elements(L), %% X always belongs to L
not lists:member(X,delete_all(X,L))))).
View proper3
27> proper:quickcheck(listdel:prop_delete3()).
..x.x.....x...............!
Failed: After 24 test(s).
[13,13,59,10,11,24,-3,0,4]
13
Shrinking ...(3 time(s))
[13,13]
13
false
View delete_all2.erl
delete_all(_, [], Acc) ->
lists:reverse(Acc);
delete_all(X, [X|Rest], Acc) ->
delete_all(X, Rest, Acc);
delete_all(X, [Y|Rest], Acc) ->
delete_all(X, Rest, [Y|Acc]).
View proper4
4> proper:quickcheck(listdel:prop_delete3()).
.xx..xxx........................x.....x............x...........................x.........x..........x....x......
OK: Passed 100 test(s).
true
5> proper:quickcheck(listdel:prop_delete3(), 1000).
xxx..xx...x.xxxx..xxx.xx...x..xxxx.........x.x.........x...x..........x......x.......x....x...xx...x........x......x......x....x.......x.................x....x......x...........x.......x.....x.x..........x........x.x.................x..x.......................x..........x............x.......x......................................x.......................................x.......x.........x..x...................x..................................................................................x........x.....x..........................................................................x..x.......x..................................x......................x........x..................x...x.........x.x.....................................x.x...........................................x
You can’t perform that action at this time.