Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Karel (programming language)

From Wikipedia, the free encyclopedia
Programming language
Karel the Robot
Paradigmprocedural
Designed byRichard E. Pattis
First appeared1981
Websitexkarel.sourceforge.net
Dialects
Some localized language variants
Influenced by
Pascal
Influenced
Karel++,AgentSheets,Guido van Robot

Karel is aneducational programming language for beginners, created byRichard E. Pattis in his bookKarel The Robot: A Gentle Introduction to the Art of Programming. Pattis used the language in his courses atStanford University,California. The language is named afterKarel Čapek, aCzech writer who introduced the wordrobot in his playR.U.R.[1][2]

Principles

[edit]

Aprogram in Karel is used to control a simplerobot named Karel that lives in an environment consisting of a grid of streets (left-right) and avenues (up-down). Karel understands five basic instructions:move (Karel moves by one square in the direction he is facing),turnLeft (Karel turns 90 ° left),putBeeper (Karel puts a beeper on the square he is standing at),pickBeeper (Karel lifts a beeper off the square he is standing at), andturnoff (Karel switches himself off, the program ends). Karel can also performboolean queries about his immediate environment, asking whether there is a beeper where he is standing, whether there are barriers next to him, and about the direction he is facing. Aprogrammer can create additionalinstructions by defining them in terms of the five basic instructions, and by using conditionalcontrol flow statementsif andwhile with environment queries, and by using theiterate construct.[citation needed]

Example

[edit]

The following is a simple example of Karel syntax:

BEGINNING-OF-PROGRAM  DEFINE turnRight AS BEGIN   turnLeft;   turnLeft;   turnLeft; END  BEGINNING-OF-EXECUTION   ITERATE 3 TIMES   BEGIN     turnRight;     move   END   turnoff END-OF-EXECUTION END-OF-PROGRAM

Specification

[edit]

The following implementation is Karel in thePython programming language.[3] Other implementations are available.

Primitive functions

[edit]

The following are the primitive functions.

move()
Karel moves one square in the direction it is facing.
turn_left()
Karel turns left by 90 degrees.
put_beeper()
Karel puts a beeper on its current square.
pick_beeper()
Karel picks up a beeper from its current square.
paint_corner(COLOR_NAME)
Karel paints its current corner with a color. There is a finite list of available colors.

Program Structures

[edit]

Karel programs are structured in the following way:

  • Comments: Any line starting with# is a comment and is ignored by the interpreter.
  • Functions in Karel are declared usingdef, followed by the function name and parentheses. The body of the function follows in subsequent lines.
  • main(): A program run executes themain function. The other functions are not executed unless called.

Conditions in Karel

[edit]

Karel can respond to certain conditions in its world:

front_is_clear(),beepers_present(),beepers_in_bag(),left_is_clear(),right_is_clear(),facing_north(),facing_south(),facing_east(), andfacing_west()

And their inverses:

front_is_blocked(),no_beepers_present(),no_beepers_in_bag(),left_is_blocked(),right_is_blocked(),not_facing_north(),not_facing_south(),not_facing_east(), andnot_facing_west().

It can also check the current block's color bycorner_color_is(COLOR_NAME).

Conditions and Loops

[edit]

There are two control structures:

  • Conditional execution usingif andelse.
  • Loops usingfor andwhile.

Variants and descendants

[edit]

The language has inspired the development of various clones and similar educational languages. As the language is intended for beginners, localized variants exist in some languages, notablyCzech (the programming language was quite popular inCzechoslovakia).

The principles of Karel were updated to theobject-oriented programming paradigm in a new programming language called Karel++. Karel++ is conceptually based on Karel, but uses a completely new syntax, similar toJava.

A REALbasic implementation, rbKarel,[4] provides the basic Karel commands within an RBScript environment with BASIC syntax being used for loops and conditionals. This teaching project provides a cross-platform GUI for Karel experiments including single-stepping and spoken output.

A Karel-inspired language and environment calledRobot Emil[5] uses a3D view of the robot's world.Robot Emil offers a large palette of objects that can be placed to depict walls, windows (transparency), water and grass. The camera may be moved freely throughout the 3D environment. The robot may be controlled interactively with buttons in the GUI, or by programs written in Emil's Karel-like programming language. The author states that the program is free for use by schools, students and children. Versions are available inEnglish,Czech andSlovak.

A proprietary language which is also called Karel is used to program the robots ofFANUC Robotics. However, FANUC Karel is derived fromPascal.

The language has also been implemented as Karel the Dog inJavaScript byCodeHS. Similar to the original language, this implementation features Karel in a grid world. Programmers use and build upon Karel's simple vocabulary of commands to accomplish programming tasks. Instead of putting and picking beepers, Karel the Dog puts and takes tennis balls.

A German version of Karel is named "Robot Karol".[6]

See also

[edit]

Further reading

[edit]
  • Richard E. Pattis.Karel The Robot: A Gentle Introduction to the Art of Programming. John Wiley & Sons, 1981.ISBN 0-471-59725-2.
  • Joseph Bergin, Mark Stehlik, Jim Roberts, Richard E. Pattis.Karel++: A Gentle Introduction to the Art of Object-Oriented Programming. John Wiley & Sons, 1996.ISBN 0-471-13809-6.

References

[edit]
  1. ^"Who did actually invent the word 'robot' and what does it mean?". Archived fromthe original on 27 July 2013. Retrieved25 July 2013.
  2. ^Margolius, Ivan (Autumn 2017)"The Robot of Prague"Archived 11 September 2017 at theWayback MachineThe Friends of Czech Heritage Newsletter no. 17, pp.3-6
  3. ^Piech, Chris; Roberts, Eric (January 2019)."Karel the Robot Learns Python. Appendix: Reference".compedu.stanford.edu. Retrieved2023-06-12.
  4. ^"Google Code Archive - Long-term storage for Google Code Project Hosting".
  5. ^"Introduction (EN)".www.emil.input.sk. Archived fromthe original on 2010-11-02.
  6. ^"Robot Karol – Escape the Maze | Schülerlabor Informatik - InfoSphere, Informatik entdecken in Modulen für alle Schulformen & Klassenstufen".schuelerlabor.informatik.rwth-aachen.de. Retrieved29 December 2020.

External links

[edit]
Authority control databases: NationalEdit this at Wikidata
Retrieved from "https://en.wikipedia.org/w/index.php?title=Karel_(programming_language)&oldid=1320362726"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp