Inconsistent syntax of "case"

Started by seetho, January 12, 2009, 07:12:57 PM

Previous topic - Next topic

DrDave

#15
Quote from: "seetho"My original gripe was that exp-1 implied that it was evaluated.  It would've been better to call it const-1 so that you get:


(case exp-switch (const-1 body-1) [(const-2 body-2) ... ])

You see "const" then you know that you cannot put an expression there. I know it seems a petty matter to those already familiar with the concept, but to newbies like me emerging from decades of languishing in the "dark side" of imperative languages, it does make things easier to grasp.

I think you make a good point here for a change to the wording in the manual. In fact, along with some additional clarification, because even if you try to force evaluatioin with eval, it fails. Also, if you set a symbol as a constant and try to use that constant,  thinking that a constant symbol would surely work, it also fails. So as far as I can tell, you must write the actual number  or string to match. I think I might want to be able to evaluate an expresion on the fly whose result is what case is being matched against. For example, perhaps I would like my case statements to be something like
("item-1" "found 1")
("item-2" "found 2")
And I have a hundred of these.

I would like to code my case statement something like this
; do some code...
(setf x 2)
; do some code...
(setf mystring "item-"
(case str-to-match
         ((string mystring x) (string "found " x) ))

But of course it won't give the desired output, because the way case is currently implemented, it won't evaluate the first part.
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.

\"Getting Started with Erlang\"  version 5.6.2

Kazimir Majorinc

#16
Quote from: "DrDave"I would like to code my case statement something like this
; do some code...
(setf x 2)
; do some code...
(setf mystring "item-"
(case str-to-match
         ((string mystring x) (string "found " x) ))


I think letex is perfect for that. Something like (but I didn't tested it)


(letex ((y (string mystring x)))
       (case str-to-match
            (y (string "found" x))))


Lutz did few such things really hitting the target.
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

DrDave

#17
Quote from: "Kazimir Majorinc"
I think letex is perfect for that. Something like (but I didn't tested it)


(letex ((y (string mystring x)))
       (case str-to-match
            (y (string "found" x))))


Lutz did few such things really hitting the target.

Now that is very interesting! Why does letex create a variable that seems to get evaluated?



This indeed works
(for (x 1 5)
(begin
  (setq mystring "item-" )
  (setq str-to-match (string "item-" x))
  (letex ((y (string mystring x)))
       (case str-to-match
            (y (println (string "found " x)))
))))




-->

found 1

found 2

found 3

found 4

found 5
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.

\"Getting Started with Erlang\"  version 5.6.2

Kazimir Majorinc

#18
Why does letex create a variable that seems to get evaluated?



Because letex creates new expression such that x is replaced with its value, and evaluates that new expression. These two are equivalents (letex = let & expand.)


(println (letex ((x 3))
                (+ x 3)))
               
(println (let((x 3))
             (eval (expand '(+ x 3)
                           'x))))
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

DrDave

#19
So just to clarify,  we can use letex to set a variable to use in the case selections, but we can't put an expression directly in place of the variable.


(set 'n 3 'x 24 'y 8)
(letex ((m (/ x  y)))
(println (case n
              (1 "one")
              (2 "two")          
              (m  "three")
              (4 "four"))))
--> "three"
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.

\"Getting Started with Erlang\"  version 5.6.2

DrDave

#20
Quote from: "Kazimir Majorinc"Why does letex create a variable that seems to get evaluated?



Because letex creates new expression such that x is replaced with its value, and evaluates that new expression. These two are equivalents (letex = let & expand.)


(println (letex ((x 3))
                (+ x 3)))
               
(println (let((x 3))
             (eval (expand '(+ x 3)
                           'x))))

Ah, now I see. It is similar to passing arguments to a function by value rather than by reference. When we use expand, the VALUE bound to the symbol replaces the symbol in the expression, and then this new expression containing the values gets evaluated.
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.

\"Getting Started with Erlang\"  version 5.6.2

seetho

#21
Quote from: "DrDave"
Now that is very interesting! Why does letex create a variable that seems to get evaluated?


Functions are left associative so, the letex expression gets evaluated first, so all y gets substituted and evaluated to its final value before case get evaluated, and by then it sees the final value there as a constant.