GithubHelp home page GithubHelp logo

How is `cond` supposed to work? about mal HOT 2 OPEN

metaleap avatar metaleap commented on September 27, 2024
How is `cond` supposed to work?

from mal.

Comments (2)

metaleap avatar metaleap commented on September 27, 2024

Well, I wrote one from scratch that words for me and expands correctly

  • just not variadic which I don't much care for anyway;
  • minor idiosyncrasies: my MAL doesn't have defmacro, only def, and my false/true/nil are :keywords
(def caseOf
	(macro (cases)
		(if (isEmpty cases)
			:nil
			(let (	(this_case (nth cases 0))
						(case_cond (nth this_case 0))
						(case_then (nth this_case 1)))
				`(if ~case_cond
						~case_then
						(caseOf ~(rest cases)))))))

Seems to work well:

เฟŠ  (macroExpand (caseOf [ [:false (do (print :nay) :nope)] [:true (do (print :yay) (+ 1 2))] ]))
(if :false (do (print :nay) :nope) (caseOf ([:true (do (print :yay) (+ 1 2))])))

เฟŠ  (caseOf [ [:false (do (print :nay) :nope)] [:true (do (print :yay) (+ 1 2))] ])
:yay3

Anyone see any issues with that? Also, still eager to learn what I was missing about MAL's own cond! Surely it isn't actually butchered and just looked that way to under-informed me (and my MAL impl).

from mal.

metaleap avatar metaleap commented on September 27, 2024

Update: only now noticed that MAL's if has the 3rd arg as optional, missed that part before. Still, having updated my MAL impl to use :nil for a missing 3rd if arg, the cond as originally formulated:

(def cond
  (macro (& xs)
    (if (> (count xs) 0)
      (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw "odd number of forms to cond")) (cons 'cond (rest (rest xs)))))))

still results in a stack overflow with 2 args or more ๐Ÿ˜ตโ€๐Ÿ’ซ my own caseOf formulation works though and as before, all other tests pass ยฏ\_(ใƒ„)_/ยฏ

from mal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.