scheme_prelude.erl 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. -module(scheme_prelude).
  2. -export([cons/2, car/1, cdr/1, listxqs/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. xpl([A]) -> A;
  24. xpl([A|B]) -> A + xpl(B).
  25. xst([A]) -> A;
  26. xst([A|B]) -> A * xst(B).
  27. xmn([A]) -> A;
  28. xmn([A|B]) -> A - xmn(B).
  29. list(L) -> L.
  30. xeq(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. listxqs([_|B]) -> scheme_prelude:list_qs(B);
  46. listxqs([]) -> true;
  47. listxqs(_) -> false.
  48. nullxqs([]) -> true;
  49. nullxqs(_) -> false.
  50. pairxqs([_|_]) -> true;
  51. pairxqs(_) -> false.
  52. numberxqs(X) -> is_number(X).
  53. equalsxqs(X, Y) -> X == Y.
  54. length([X|Y]) -> 1 + scheme_prelude:length(Y);
  55. length([]) -> 0.
  56. symbolxqs(X) -> is_atom(X).
  57. booleanxqs(true) -> true;
  58. booleanxqs(false) -> true;
  59. booleanxqs(_) -> false.
  60. scheme_and(L) -> all(fun(X) -> scheme_prelude:is_true end, L).
  61. scheme_or(L) -> any(fun(X) -> scheme_prelude:is_true end, L).
  62. all(Func, [X|XS]) ->
  63. case Func(X) of
  64. scheme_not(false) -> true;
  65. scheme_not(_) -> false.
  66. is_true(false) -> false;
  67. is_true(X) -> X.