Racket Haskell

Racket Haskell

Racket Haskell ; Function definition and application -- Function definition and application (lambda(xy)(*xy)) \x y ->x*y (+345) ; 12 (==)3(4- 25) -- False ((lambda(x)(+3x)) 10) ; 13 (\x ->x+3) 10 -- 13 ; Name bindings -- Name bindings (definex 10) x= 10 (define(fz)(first(restz))) fz=z+ 10 (let*([y(+ 10 20)] lety= 10+ 20 [z(+ 10y)]) z= 10+y (*yz)) iny*z ; Syntactic forms -- If (note that && and || are just functions) (and #f(/10)) ; #f fx= (or #t(/10)) ; #t ifx == 10 (if #t then 16 42 else-20 (/10)) ; 42 (cond[#f(/10)] [#t 42] [(/10) 25] [else1]) ; 42 ; Pattern matching -- Pattern matching (define/match(commentx) comment7="Lucky" [(7) "Lucky"] comment 13="Unlucky" [(13) "Unlucky"] comment_="Other" [(_) "Other"]) (define/match(f lst) f[]= 100 [((list)) 100] f (x:xs)=x+ length xs [((consx xs)) (+x(length xs))]) ; Lists -- Lists (cons2(cons3 null)) ; '(2 3) 2:3:[] -- [2,3] (list12 20) ; '(1 2 20) [1,2,20] -- [1,2,20] (first(list123)) ; 1 head[1,2,3] -- 1 (rest(list123)) ; '(2 3) tail[1,2,3] -- [2,3] (null? null) ; #t null[] -- True (length(list1 205)) ; 3 length[1,2,20] -- 3 (append(list12)(list46) ; '(1 2 4 6) [1,2] ++[4,6] -- [1,2,4,6] (member2(list123)) ; '(2 3) elem2[1,2,3] -- True (member4(list123)) ; #f elem4[1,2,3] -- False (list-ref(list2 401)1) ; 40 [20,40,1]!!1 -- 40 (map(lambda(x)(*3x)) map(\x ->3* x) [1,2,3] -- [3,6,9] (list123)) ; '(3 6 9) (filter(lambda(x)(<3x)) filter(\x ->3< x) [10,-4,15] -- [10,15] (list 10 -4 15)) ; '(10 15) (foldl+ 15(list123)) ; (3 + (2 + (1 + 15))) foldl(+) 15[1,2,3] -- (((15 + 1) + 2) + 3) (apply-(list 163)) ; 13 f$x=fx -- unary function application ; Maps -- Maps (hash) ; fg Map.empty -- fg (hash "a"1 "b"2) ; fa: 1, b: 2g Map.fromMap [("a",1), ("b",2)] -- fa: 1, b: 2g (hash-has-key? hash key) ; returns boolean Map.member key map -- returns Bool (hash-set hash key value) ; returns new map Map.insert key value map -- returns new map (hash-ref hash key) ; map lookup map! key -- map lookup Streams, Continuations, and Choice Haskell types ; Streams -- Type annotations empty-stream ; empty stream True:: Bool (define-syntax stream-cons (&&):: Bool -> Bool -> Bool (syntax-rules() head:: [a] ->a [(stream-cons <first> <rest>) (==):: Eqa =>a ->a -> Bool (cons(thunk <first>)(thunk <rest>))])) 1:: Numa =>a -- multiple type class constraints (define(stream-first stream) ((car stream))) myFunc::(Eq a, Num b) =>a ->b -> Bool (define(stream-rest stream) ((cdr stream))) ; Choice implementation -- Type declarations, type synonyms (define choices null) ; a stack of choices data Point= Point Integer Integer (define DONE 'done) data BTreea= Empty| BTreea(BTree a) (BTree a) type String=[Char] (define-syntax -< (syntax-rules() -- Type class instantiation [(-< <expr1>) <expr1>] instance Show Point where [(-< <expr1> <expr2>...) show (Point x y)= (shiftk "(" ++ (show x) ++"," ++ (show y) ++")" (add-choice!(thunk(k(-< <expr2>...)))) (k <expr1>))])) -- Functors and monads class Functorf where (define(next!) fmap:: (a -> b) ->fa ->fb (if(null? choices) (shiftk DONE) class Monadm where (reset((get-choice!))))) (>>=)::ma -> (a -> m b) ->mb return::a ->ma (define(backtrack!)(shiftk(next!))) -- Modeling failing computations data Maybea= Nothing| Justa -- data Eitherab= Lefta| Rightb -- Modeling mutation data Statesa= State (s -> (a, s)) state:: (s -> (a, s)) -> Statesa runState:: Statesa ->s -> (a, s) runState(State f) init= f init get:: Statess get= State(\state -> (state, state)) put::s -> States() putx= State(\_ ->((), x)) -- Standard input/output actions getLine::IO String -- read line from stdin, -- not including newline putStrLn:: String ->IO() -- print string to stdout, -- with terminating newline -- NOTE: Maybe, (Either a), (State s), [], and IO -- are all instances of Functor and Monad..

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    2 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us