Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheme Switch-Statement Syntax

What is the smartest way to create a switch statement in Scheme?

I want to check one value up against several others, if one results true the entire function should result true, otherwise false. I am not very good with the syntax in scheme.

like image 720
Aveneon Avatar asked May 11 '15 15:05

Aveneon


People also ask

What is the syntax for switch statement?

A typical syntax involves: the first select , followed by an expression which is often referred to as the control expression or control variable of the switch statement. subsequent lines defining the actual cases (the values), with corresponding sequences of statements for execution when a match occurs.

What is switch statement example?

So, printf(“2+3 makes 5”) is executed and then followed by break; which brings the control out of the switch statement. Other examples for valid switch expressions: switch(2+3), switch(9*16%2), switch(a), switch(a-b) etc.

What is a Scheme expression?

A Scheme expression is a construct that returns a value, such as a variable reference, literal, procedure call, or conditional. Expression types are categorized as primitive or derived. Primitive expression types include variables and procedure calls.

What is a Scheme function?

A function object can be bound to a name via define like any other kind of value. But we often use a slightly different, equivalent syntax for function definitions, where the ' lambda ' is implicitly specified. (define f (lambda (p1 p2) ... ))


2 Answers

In Scheme you have case:

(case (car '(c d))
  ((a e i o u) 'vowel)
  ((w y) 'semivowel)
  (else 'consonant)) ; ==> consonant

As you see it compares against literal data. Thus you cannot compare the value with other variables. Then you need cond

like image 61
Sylwester Avatar answered Sep 23 '22 03:09

Sylwester


An alternative to an explicit comparison agains each value, is to use member:

> (define (vowel? x) (member x '(a e i o u))
> (vowel? 'b)
#f
like image 28
soegaard Avatar answered Sep 21 '22 03:09

soegaard