scheme_prelude.erl 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. -module(scheme_prelude).
  2. -export([cons/2, car/1, cdr/1, list_p/1]).
  3. % Scheme prelude for programs compiled from Scheme to Erlang.
  4. % A few conventions apply:
  5. % - question marks become _p, so even? becomes even_p
  6. % - exclamation marks are outlawed because the Scheme subset
  7. % is pure-functional, so set! and its ilk are not present.
  8. % - hyphens become _mn, so list-ref becomes list_mnref
  9. % - underscores will become double underscores.
  10. % At present, this still leaves an ambiguity between even-p and
  11. % even?, but we will ignore that. All that aside, operator
  12. % symbols become three-character abbreviations whose names all
  13. % begin with an underscore. For example:
  14. % + -> _pl
  15. % * -> _st
  16. % - -> _mn
  17. % / -> _dv
  18. % Therefore, symbols can still be used in variable names, such
  19. % as *some-variable* which becomes _stsome_mnvariable_st. This
  20. % would be hideous to work with, but for our purposes is
  21. % transparent enough.
  22. % variadic functions
  23. _pl([A]) -> A;
  24. _pl([A|B]) -> A + _pl(B).
  25. _st([A]) -> A;
  26. _st([A|B]) -> A * _st(B).
  27. _mn([A]) -> A;
  28. _mn([A|B]) -> A - _mn(B).
  29. list(L) -> L.
  30. _eq(A, B) -> A == B.
  31. cons(A, B) -> [A|B].
  32. car([A|_]) -> A.
  33. cdr([_|B]) -> B.
  34. append(A, B) -> lists:append(A, B).
  35. filter(Proc, L) -> lists:filter(Proc, L).
  36. map(Proc, L) -> lists:map(Proc, L).
  37. member(X, [X|L]) -> [X|L];
  38. member(X, [_|L]) -> member(X, L);
  39. member(_, []) -> false.
  40. assoc(X, [{X, A}|L]) -> {X, A};
  41. assoc(X, [{_, _}|L]) -> scheme_prelude:assoc(X, L);
  42. assoc(_, []) -> false.
  43. reverse(L) -> lists:reverse(L).
  44. reduce(Proc, [X|L]) -> lists:foldl(Proc, X, L).
  45. list_qs([_|B]) -> scheme_prelude:list_qs(B);
  46. list_qs([]) -> true;
  47. list_qs(_) -> false.
  48. null_qs([]) -> true;
  49. null_qs(_) -> false.
  50. pair_qs([_|_]) -> true;
  51. pair_qs(_) -> false.
  52. number_qs(X) -> is_number(X).
  53. equals_qs(X, Y) -> X == Y.
  54. length([X|Y]) -> 1 + scheme_prelude:length(Y);
  55. length([]) -> 0.
  56. symbol_qs(X) -> is_atom(X).
  57. boolean_qs(true) -> true.
  58. boolean_qs(false) -> true.
  59. boolean_qs(_) -> false.
  60. and(L) -> all(scheme_prelude:is_true, L).
  61. or(L) -> any(scheme_prelude:is_true, L).
  62. not(false) -> true;
  63. not(_) -> false.
  64. is_true(false) -> false;
  65. is_true(X) -> X.