Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

A rookie Racket question. I'm using Krishnamurthi's PLAI textbook for this one, and the associated Racket programming language.

Now, let's say that I have a defined type as such:

(define-type Thingy
 [thingy (num number?)])

So, is there any circumstance at all under which I could get this thingy to accept an empty list '() ?

share|improve this question
up vote 2 down vote accepted

An empty list is not a number, so the type definition you have will not accept it.

You can use (lambda (x) (or (number? x) (null? x))) instead of number? to accept either a number or an empty list, but I have no idea why you would want to do that.

share|improve this answer

As described in http://docs.racket-lang.org/plai/plai-scheme.html, define-type can take several different variants. It can define a disjoint datatype in a way that allows the language itself to help you write safer code.

For example:

#lang plai

(define-type Thingy
 [some (num number?)]
 [none])

Code that works with Thingys now need to systematically process the two possible kinds of Thingys. When you use type-case, it will enforce this at compile time: if it sees that you have written code that doesn't account for the possible kinds of Thingy, it'll throw a compile-time error.

;; bad-thingy->string: Thingy -> string
(define (bad-thingy->string t)
  (type-case Thingy t
    [some (n) (number->string n)]))

This gives the following compile-time error:

type-case: syntax error; probable cause: you did not include a case for the none variant, or no else-branch was present in: (type-case Thingy t (some (n) (number-> string n)))

And that's right: the code has not accounted for the case of none.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["                    <li><a href=\"//area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"//careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2016 Stack Exchange Inc; user contributions licensed under <a href=\"https://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a> \r\n                with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2016.4.29.3525\r\n            </div>\r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Stack Overflow works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div>\r\n    </noscript>\r\n<scrip"], "copyright__copyright__copyright": ["the following compile-time error:\n\ntype-case: syntax error; probable cause: you did not include a case for the none variant, or no else-branch was present in: (type-case Thingy t (some (n) (number-> string n)))\n\nAnd that's right: the code has not accounted for the case of none.\n\nshare|improve this answer\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}